8

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.

Sonic Master
  • 1,238
  • 2
  • 22
  • 36

3 Answers3

18

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.

Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • 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)". – Kiran P Nair Jan 14 '21 at 19:26
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
}
impression7vx
  • 1,728
  • 1
  • 20
  • 50
  • So alamofire doesn't include those function? – Sonic Master Aug 24 '16 at 23:47
  • 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 API – impression7vx Aug 24 '16 at 23:49
  • 3
    It does resizing via filters. See https://github.com/Alamofire/AlamofireImage#image-filters-1. – Rob Aug 25 '16 at 00:17
  • Wow. Intriguing. Wish i had known about this about 2 months ago :P lol! – impression7vx Aug 25 '16 at 00:20
  • Thank you so much, @Rob! You answered my previous question too :D – Sonic Master Aug 25 '16 at 00:28
  • @impression7vx - 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. http://stackoverflow.com/a/28513086/1271826 – Rob Aug 25 '16 at 00:35
  • See. This is what SO is about. Preciate it man. Where man = @Rob – impression7vx Aug 25 '16 at 00:40
  • Yo @Rob, you should check this out, maybe get some rep while you are at it! Not that you need it :P http://stackoverflow.com/questions/39088313/only-first-track-playing-of-avmutablecomposition – impression7vx Aug 25 '16 at 00:42
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!
}
Kiran Patil
  • 402
  • 5
  • 13