最好的选择是将 SDWebImage 文件夹拖放到项目中。确保勾选“如果需要,复制项目”。
制作一个 Obj C 桥接:文件 -> 新建 -> 源 -> 头文件 -> 名称为 AppName-Bridging-Header。
添加以下内容:
#ifndef AppName_AppName_Bridging_Header_h
#define AppName_AppName_Bridging_Header_h
#import <SDWebImage/UIImageView+WebCache.h>
#import "UIImageView+WebCache.h"
#endif
or
#import "UIImageView+WebCache.h"
参考:https ://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html
注意:构建设置,在 Swift 编译器 - 代码生成中,确保下的 Objective-C Bridging Header 构建设置具有桥接头文件的路径。- 类似于 testSD/testSD-Bridging-Header.h 或 testSD-Bridging-Header.h(打开项目文件夹并找到头文件路径)
现在尝试使用以下代码:
let block: SDWebImageCompletionBlock! = {(image: UIImage!, error: NSError!, cacheType: SDImageCacheType!, imageURL: NSURL!) -> Void in
println(self)
}
let url = NSURL(string: "http://arrow_upward.com/350x150")
self.imageView.sd_setImageWithURL(url, completed: block)
假设如果您使用 UICollectionView 来填充缓存图像,请尝试使用此代码。
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = photoListCollectionView.dequeueReusableCellWithReuseIdentifier("scoutimagecellidentifier", forIndexPath: indexPath) as! ScoutImageCell
//Loading image from server using SDWebImage library
let thumbImageUrl = NSURL(string: self.photoPropertyArray[indexPath.row] as String)
//Image Fetching is done in background GCD thread
SDWebImageManager.sharedManager().downloadImageWithURL(thumbImageUrl, options: [],progress: nil, completed: {[weak self] (image, error, cached, finished, url) in
if let wSelf = self {
//On Main Thread
dispatch_async(dispatch_get_main_queue()){
cell.scoutimage.image = image
cell.photoloader.stopAnimating()
}
}
})
return cell
}