Resize Download Image with AlamofireImage
Asked Answered
H

3

9

I have 200px x 200px UIImageView on UICollectionViewCell that will display an image from URL. The problem is I don't know what resolution's image provided by the URL and I think it's better to resize it first before placing in UIImageView due to memory consumption.

I already use alamofire to download an image

let url = NSURL(string: "http:\(product.picUrl)")
self.imgProductItem.af_setImageWithURL(url!, placeholderImage: UIImage(named: "app_default-resize"))

I am wondering is there any method to resize it first before it use to place in UIImageView? And if any tips for download an image to save memory usage, I'd like to hear that.

Any help would be appreciated. Thank you.

Harleigh answered 24/8, 2016 at 23:44 Comment(0)
C
19

You can use filters:

let url = URL(string: ...)!
let placeholder = UIImage(named: "app_default-resize")
let filter = AspectScaledToFillSizeFilter(size: imageView.frame.size)
imageView.af.setImage(withURL: url, placeholderImage: placeholder, filter: filter)

See https://github.com/Alamofire/AlamofireImage#image-filters-1.

For Swift 2 version, see previous revision of this answer.

Cranberry answered 25/8, 2016 at 0:30 Comment(1)
Thank you, by using filter I can save some memory issues caused by big image. Instead of af_setImage I use "imageView.af.setImage(withURL: url, placeholderImage: placeholder,filter: filter)".Largish
R
1
func ResizeImage(image: UIImage, targetSize: CGSize) -> UIImage {

    let rect = CGRectMake(0, 0, targetSize.width, targetSize.height)

    UIGraphicsBeginImageContextWithOptions(targetSize, false, 1.0)
    image.drawInRect(rect)
    let newImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    return newImage
}
Rogers answered 24/8, 2016 at 23:46 Comment(8)
So alamofire doesn't include those function?Harleigh
I don't think Alamofire does any resizing in itself, I am pretty sure you need to obtain the image via URL or NSData then create the image, then resize it. Alamofire is more of the "get stuff from database" kind of APIRogers
It does resizing via filters. See github.com/Alamofire/AlamofireImage#image-filters-1.Cranberry
Wow. Intriguing. Wish i had known about this about 2 months ago :P lol!Rogers
Thank you so much, @Rob! You answered my previous question too :DHarleigh
@Rogers - No problem. BTW, if you're going to draw it like above, I'd suggest using a scale of 0, so it scales appropriately for the device in question. If you use 1, it will be unnecessarily pixelated on retina devices. Also, you may want to be careful regarding distortion, so you might handle aspect fill vs aspect fit vs simple scaling. https://mcmap.net/q/832225/-how-to-resize-a-bitmap-on-iosCranberry
See. This is what SO is about. Preciate it man. Where man = @CranberryRogers
Yo @Rob, you should check this out, maybe get some rep while you are at it! Not that you need it :P #39088813Rogers
U
0

swift 5 updated

func ResizeImage(image: UIImage, targetSize: CGSize) -> UIImage {
    let rect = CGRect(x: 0, y: 0, width: targetSize.width, height: targetSize.height)
    UIGraphicsBeginImageContextWithOptions(targetSize, false, 1.0)
    image.draw(in: rect)
    let newImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return newImage!
}
Upheaval answered 21/12, 2020 at 11:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.