2016-08-24 45 views
3

我有200px x 200px UIImageViewUICollectionViewCell將顯示來自URL的圖像。問題是我不知道URL提供的分辨率圖像是什麼,我認爲最好在放入UIImageView之前調整它的大小,這是由於內存消耗。調整大小下載圖片AlamofireImage

我已經使用alamofire下載圖像

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

我想知道是否有任何先調整之前,用它在UIImageView放置方法?如果有任何提示下載圖像以節省內存使用量,我想聽聽。

任何幫助,將不勝感激。謝謝。

回答

0
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 
} 
+0

那麼alamofire不包括那些功能? –

+0

我不認爲Alamofire自己做任何調整大小,我很確定你需要通過URL或NSData獲取圖像,然後創建圖像,然後調整它的大小。 Alamofire更像是「從數據庫中獲取東西」的API – impression7vx

+2

它通過過濾器來調整大小。見https://github.com/Alamofire/AlamofireImage#image-filters-1。 – Rob