Swift 中使用SDWebImage
程序员文章站
2022-06-01 10:47:16
...
SDWebImage是开发中常用的插件,主要用于加载外部图片,具有缓存图片资源的功能,然而SDWebImage是由Object-C所写。
步骤:
- 下载SDWebImage , https://github.com/rs/SDWebImage
- 4.0版本还需要下载FLAnimatedImage, https://github.com/Flipboard/FLAnimatedImage
- 将上面两个库中的源码加到你自己的项目中,如图
- 添加一个Object-c和swift的桥接,新建一个头文件,名叫SDWebImage+Header.h ,添加SDWebImage头文件的引用,
#ifndef SDWebImage_Header_h
#define SDWebImage_Header_h
#endif /* SDWebImage_Header_h */
#import "UIImageView+WebCache.h"
let imageURL = URL.init(string: "http://www.szplanner.com/images/inside/product_activity_thumb.jpg")
SDWebImageManager.shared().loadImage(with: imageURL, options: SDWebImageOptions.highPriority, progress: {(receivedSize:Int,expectedSize:Int,targetURL:URL?)->Void in
let pro = Float(receivedSize)/Float(expectedSize)*100
print("进度..\(pro)%")
}, completed: { ( image:UIImage?,data:Data?, error:Error? ,cacheType:SDImageCacheType, finished:Bool,url:URL?) -> Void in
if let img = image {
self.imgView = UIImageView(image: img)
self.view.addSubview(self.imgView)
}
})
上一篇: uboot启动流程分析
下一篇: Centos6.5搭建sftp服务器