Re: Scaling a UIImage
Re: Scaling a UIImage
- Subject: Re: Scaling a UIImage
- From: David Duncan via Cocoa-dev <email@hidden>
- Date: Mon, 02 Nov 2020 15:09:47 -0800
> On Nov 2, 2020, at 2:59 PM, Carl Hoefs via Cocoa-dev
> <email@hidden> wrote:
>
> How can I correctly scale a UIImage from 3264x2448 down to 640x480 pixels?
>
>
> I have an iOS app that interacts with a macOS server process. The iOS app
> takes a 3264x2448 camera image, scales it to 640x480 pixels, and makes a JPEG
> representation of it to send to the server:
>
> NSData *dataObj = UIImageJPEGRepresentation(origImage,0.5);
>
> The server reads the bytes, and creates an NSImage:
>
> NSImage *theImage = [[NSImage alloc] initWithData:imageData];
> NSBitmapImageRep *imageRep = [[NSBitmapImageRep alloc]
> initWithData:[theImage TIFFRepresentation]];
Do you specifically need an NSBitmapImageRep? What is your next step here.
Also any code using -TIFFRepresentation for any reason other than to get actual
TIFF data is likely suspect. There are absolutely more efficient ways to do
this.
But which way you should do depends on your next step...
>
> But at this point, imageRep.pixelsWide=1280 and imageRep.pixelsHigh=960!
You got 1280x960 because your on a 2x device and your resizing code didn’t
specify a scale for the image (you passed 0.0, which means to use “native”
scale).
>
> If I write theImage to disk and look at it with Preview, it displays onscreen
> as 640x480 but Preview's Inspector Tool shows it to be 1280x960.
>
> On the iOS app side, here's the UIImage category method I'm using to scale
> the image:
>
> + (UIImage *)imageWithImage:(UIImage *)image
> scaledToSize:(CGSize)newSize
> {
> UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
Explicitly pass 1 here.
> [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
> UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
> UIGraphicsEndImageContext();
> return newImage;
> }
>
> Any suggestions?
> -Carl
>
> _______________________________________________
>
> Cocoa-dev mailing list (email@hidden)
>
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden