UIImage-Resize
UIImage-Resize copied to clipboard
Use 0.0 instead of self.size as the scale, defaulting to device scale.
See https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIKitFunctionReference/#//apple_ref/c/func/UIGraphicsBeginImageContextWithOptions for details
That's actually probably what we want.
For example imagine an screenshot taken from a retina iPhone (Power+Home), saved in the Camera Roll, then transferred via email or whatnot to a non-retina iPhone.
The UIImage's scale will then be 2.0 even when opened from the non-retina iPhone, and if that non-retina iPhone wants to resize the original image, we should not downscale its resolution/scale to 1.0 just because this retina image is opened on a non-retina device.
The UIImage you resize may even never be displayed on the screen, e.g. imagine a retina image downloaded from the web, resized with my code on a non-retina iPhone, then sent back to a WebService without even being displayed on that non-retina iPhone, just being manipulated via code…
That's the reason why I used the image's scale instead of the device scale, because we manipulate the image itself, independently of the device the code is executing on.
I understand what you mean, but in my case - downscaling album art images to display in "bottom" mode inside a UIImageView, I do want it to use the device's scaling...
Could we add another method with "useDeviceScaling:YES/NO"?
That would be a better option, yes. :+1:
What I would recommend for the new API:
- Move the code to a new method named
-(UIImage*)resizedImageToSize:(CGSize)dstSize forceScale:(CGFloat)scaleand use thescaleparameter instead ofself.scalein the code; - Keep
-(UIImage*)resizedImageToSize:(CGSize)dstSizebut as a convenience method that simply calls this new designated-resizedImageToSize:forceScale:method usingself.scalefor its second parameter. That way this method will still exist and still work the same as before; - Update the documentation in the header (especially to answer the question that if we use
dstSize={50,50}andforceScale=2.0on a non-retina iPhone, will it lead to an image of size50x50 pointsor50x50 pixelsor100x100 pointsetc. to clarify such uses)