2016-11-09 82 views
1

我是Swift3編程的新手,已經接管並試圖重構Swift 2.2到Swift 3中的現有應用程序。我發現Alamofire有一個新庫AlamofireImage重構AlamofireImage in Swift 3

在那個庫中,我看到有一個imageCache,並且正在考慮是否應該重構當前的imageCache代碼以使用它。我的問題是它是否比目前的實施具有主要的優勢或效率,如果是的話,它們是什麼?任何其他改進此代碼的建議也會受到讚賞。

let imageCache = AutoPurgingImageCache()似乎是一個更簡單和可讀的實現。

這是當前的imageCache。

class ShoppingManager { 
    static let sharedManager = ShoppingManager() 
    let imageCache = NSCache<AnyObject, AnyObject>() 

這是原始的Swift 2代碼來加載圖像和緩存它。

func collectionView(_ collectionView: UICollectionView, cellForItemAtIndexPath indexPath: IndexPath) -> UICollectionViewCell { 

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellIdentifier, for: indexPath) as! ProductsCollectionViewCell 

    // Configure the cell 
    cell.lblProduct.text = "" 
    cell.backgroundColor = UIColor.white 
    cell.imgProduct.image = nil 

    //load Cell Image 

    let shopItem = shopItems[indexPath.row] 
    cell.alamoFireRequest?.cancel() 

    if let image = ShoppingManager.sharedManager.imageCache.object(forKey: shopItem.imgUrl! as AnyObject) as? UIImage { 
     cell.imgProduct.image = image 
    } else { 
     cell.alamoFireRequest = Alamofire.request(.GET, shopItem.imgUrl!).response(completionHandler: { (_, _, data: Data?, error: NSError?) -> Void in 
      if error == nil && data != nil { 
       let img = UIImage(data: data!) 
       cell.imgProduct.image = img 
       ShoppingManager.sharedManager.imageCache.setObject(img!, forKey: shopItem.imgUrl!) 
      } 
     }) 
    } 
return cell 

} 

這是我已經重構到現在的Swift3代碼。

func collectionView(_ collectionView: UICollectionView, cellForItemAtIndexPath indexPath: IndexPath) -> UICollectionViewCell { 

     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellIdentifier, for: indexPath) as! ProductsCollectionViewCell 

     // Configure the cell 
     cell.lblProduct.text = "" 
     cell.backgroundColor = UIColor.white 
     cell.imgProduct.image = nil 

     //load Cell Image 

     let shopItem = shopItems[indexPath.row] 
     cell.alamoFireRequest?.cancel() 

    if let image = ShoppingManager.sharedManager.imageCache.object(forKey: shopItem.imgUrl! as AnyObject) as? UIImage { 
     cell.imgProduct.image = image 
    } else { 
     cell.alamoFireRequest = Alamofire.request(shopItem.imgUrl!).responseImage { response in 
      guard let img = response.result.value else { 
      // Handle error 
      return 
     } 
      cell.imgProduct.image = img 
      ShoppingManager.sharedManager.imageCache.setObject(img, forKey: shopItem.imgUrl! as AnyObject) 
     }    
    } 

    return cell 

    } 

回答

2

你檢查了他們的示例代碼。

作爲%的抽樣碼,這是很容易:

imageView.af_setImage(
      withURL: URL(string: URLString)!, 
      placeholderImage: placeholderImage, 
      filter: AspectScaledToFillSizeWithRoundedCornersFilter(size: size, radius: 20.0), 
      imageTransition: .crossDissolve(0.2) 
) 


imageView.af_cancelImageRequest() 

參考:https://github.com/Alamofire/AlamofireImage/blob/master/Example/ImageCell.swift

或結帳AlamofireImageView:​​

public func af_setImage

此方法支持高速緩存。

+0

是的,謝謝。我明白這很容易。緩存當前實現爲NSCache,我想知道是否有很好的理由將緩存更改爲AlamofieImage緩存。 – markhorrocks

+0

我發現,或者我們應該遵循Alamofire,它是完全的包裝,否則不要使用Alamofire(與AFNetworking類似)。問題在於混合造成複雜性和調試問題。 –