2012-10-17 79 views
9

之前我获取了很多从网上的图片,而且都是那种规模的 - 他们可以是大,小等。SDWebImage处理图像缓存

所以我可以调整它们的大小,当我在显示它们细胞,但这是低效的。在SDWebImage下载它们并缓存它们的大小后,调整它们的大小会更好,而不是将大图像存储在磁盘上并为每个单元调整大小。

那么我该如何使用SDWebImage来做到这一点,或者我必须对该类进行一些修改?

回答

4

我和你有同样的问题,并尝试先调整SDWebImage,但最终建立了自己的组件来解决问题。你可以在这里看看它:https://github.com/adig/RemoteImageView

+0

这是伟大的!图书馆干得不错。有一些烦恼,比如在每个reloadData消息上重新加载图像,或者在每个表滚动中重新出现图像。你可以看看这个。谢谢,祝贺! – Devfly

+0

感谢您的反馈。如果您在图像加载时指向动画序列,则可以使用animate属性禁用该动画序列。 – adig

+0

好的,我已经实现了。 – Devfly

24

SDWebImage开发人员Olivier Poitrey回答了这个问题对我here

您必须实现SDWebImageManagerDelegate协议,然后将其设置为共享经理的委托是这样的:

SDWebImageManager.sharedManager.delegate = self; 

using the imageManager:transformDownloadedImage:withURL: instance method. 

More information.

完全为我工作。

+1

这真棒!非常感谢你指点我正确的方向。 – kleo

+0

抱歉没有得到。我为每个单元格的图像使用UIImageView + WebCache.h类的sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)占位符方法。我该怎么做,你可以用通俗的话来说。 –

0

SDWebImage 3.8.2

如果使用的UIImageView类sd_setImageWithURL。我已经创造了另一个的UIImageView类(扩展)

func modifiedImageFromUrl(url: NSURL?) { 
    self.sd_setImageWithURL(url) { (image, error, cacheType, url) in 
     if cacheType == SDImageCacheType.None && image != nil { 
      dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0)) { 
       let modifiedImage = // modify image as you want 

       dispatch_async(dispatch_get_main_queue()) { 
        SDWebImageManager.sharedManager().saveImageToCache(modifiedImage, forURL: url) 

        self.image = modifiedImage 
       } 
      } 
     } 
    } 
} 
0

斯威夫特3对大师的回答扩展:

myImageView.sd_setImage(with: imageUrl){ (image, error, cacheType, url) in 
    guard let image = image, cacheType == .none else { return } 

    DispatchQueue.global(qos: .userInitiated).async { 
     let modifiedImage = myImageProcessor(image) 
     SDWebImageManager.shared().saveImage(toCache: modifiedImage, for: imageUrl) 
     DispatchQueue.main.async { 
      myImageView.image = modifiedImage 
      myImageView.setNeedsDisplay() 
     } 
    } 
}