UIImage-Resize icon indicating copy to clipboard operation
UIImage-Resize copied to clipboard

Use 0.0 instead of self.size as the scale, defaulting to device scale.

Open faceleg opened this issue 10 years ago • 3 comments

See https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIKitFunctionReference/#//apple_ref/c/func/UIGraphicsBeginImageContextWithOptions for details

faceleg avatar Mar 12 '15 09:03 faceleg

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.

AliSoftware avatar Mar 12 '15 09:03 AliSoftware

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"?

faceleg avatar Mar 12 '15 10:03 faceleg

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)scale and use the scale parameter instead of self.scale in the code;
  • Keep -(UIImage*)resizedImageToSize:(CGSize)dstSize but as a convenience method that simply calls this new designated -resizedImageToSize:forceScale: method using self.scale for 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} and forceScale=2.0 on a non-retina iPhone, will it lead to an image of size 50x50 points or 50x50 pixels or 100x100 points etc. to clarify such uses)

AliSoftware avatar Mar 12 '15 10:03 AliSoftware