I’lll dig up a utility class that I made for UIImage that has this as well as others.
Cheers. > On Nov 2, 2020, at 4:59 PM, Carl Hoefs via Cocoa-dev > <cocoa-dev@lists.apple.com> 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]]; > > 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 (Cocoa-dev@lists.apple.com) > > 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: > https://lists.apple.com/mailman/options/cocoa-dev/zav%40mac.com > > This email sent to z...@mac.com _______________________________________________ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com