Scaling a UIImage
Scaling a UIImage
- Subject: Scaling a UIImage
- From: Carl Hoefs via Cocoa-dev <email@hidden>
- Date: Mon, 2 Nov 2020 14:59:25 -0800
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]];
But at this point, imageRep.pixelsWide=1280 and imageRep.pixelsHigh=960!
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);
[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