Alamofire / AlamofireImage

AlamofireImage is an image component library for Alamofire
MIT License
3.99k stars 523 forks source link

Add scale parameter to scale methods to allow setting the output image's scale. #360

Closed felipeferri closed 4 years ago

felipeferri commented 5 years ago

Goals :soccer:

At some point I needed a way of cropping an image and setting its size in pixels. I couldn't use af_imageAspectScaled for that because internally it would use the device's main screen scale factor. So, for example, if i scaled an image to the size CGSize(width: 320, height: 320), on an iPhone 6 the resulting image would have the dimensions 640x640 (in pixels) and in an iPhone 6+ the dimenstions would be 960x960.

My goal on this pull request is to allow explicitly setting the scale when scaling images.

Implementation Details :construction:

Modified the methods af_imageScaled(to size:), af_imageAspectScaled(toFill size:) and af_imageAspectScaled(toFit size:) to receive an additional parameter scale: CGFloat, which defaults to 0.0 (which is the defaul value set on UIGraphicsBeginImageContextWithOptions()).

Testing Details :mag:

I've added the test UIImageTestCase.testThatImageIsScaledWithProperScale.

cnoon commented 4 years ago

Hi @felipeferri, thank you for taking the time to put this together...much appreciated! 🍻

Instead of taking these changes alone, I've decided to rework the approach. I've just pushed change 61a029d into master which will go out shortly in AFI 4.0.0. It changes all the UIImage extensions to expose a scale parameter which defaults to nil allowing you to override the scale value used in UIGraphicsBeginImageContextWithOptions API. I've also changed the default behavior to no longer use a value of 0.0, but instead the scale of the image. This should result in much more consistent and expected behavior across the board. It was never really correct to have these functions setting the scale value to UIScreen.main.scale.

As a secondary addition, I've added the ability to customize the ImageResponseSerializer per download through the ImageDownloader and extension APIs on UIImageView and UIButton. This allows you to change the scale factor of the image download through both APIs which could only be done previously if you used the Alamofire.Session directly which no one wants to actually do. This should make the APIs much more flexible to do what you want in all situations regardless of whether you're using an Alamofire.Session, AlamofireImage.ImageDownloader, or UIImageView or UIButton extension.

Cheers. 🍻

felipeferri commented 4 years ago

Thanks for the feedback. Nice work!