看资料,好晕,自己写了个demo,总结一下
-
先集成 FastImageCache
-
appDelegate要实现代理
func configFastImageCache(){
var mutableImageFormats = [AnyObject]()
let squareImageFormatMaximumCount = 400
let squareImageFormatDevices:FICImageFormatDevices = [FICImageFormatDevices.Phone, FICImageFormatDevices.Pad]
// 16 bgr
let squareImageFormat16BitBGR = FICImageFormat(name: FICDPhotoSquareImage16BitBGRFormatName, family: FICDPhotoImageFormatFamily, imageSize: FICDPhotoSquareImageSize, style: FICImageFormatStyle.Style16BitBGR, maximumCount: squareImageFormatMaximumCount, devices: squareImageFormatDevices, protectionMode: FICImageFormatProtectionMode.None)
mutableImageFormats.append(squareImageFormat16BitBGR)
let sharedImageCache = FICImageCache.sharedImageCache()
sharedImageCache.delegate = self
sharedImageCache.setFormats(mutableImageFormats)
}
// ++++ FastImageCache FICImageCacheDelegate ++++++++++
func imageCache(imageCache: FICImageCache!, wantsSourceImageForEntity entity: FICEntity!, withFormatName formatName: String!, completionBlock: FICImageRequestCompletionBlock!) {
// Images typically come from the Internet rather than from the app bundle directly, so this would be the place to fire off a network request to download the image.
// For the purposes of this demo app, we'll just access images stored locally on disk.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
if let cd = entity as? FICDPhoto{
let filePathURL = cd.sourceImageURLWithFormatName(formatName)
let sourceData = NSData(contentsOfURL: filePathURL)
let sourceImage = UIImage(data: sourceData!)
dispatch_async(dispatch_get_main_queue(), {
completionBlock(sourceImage)
})
}
}
}
func imageCache(imageCache: FICImageCache!, shouldProcessAllFormatsInFamily formatFamily: String!, forEntity entity: FICEntity!) -> Bool {
return false
}
func imageCache(imageCache: FICImageCache!, errorDidOccurWithMessage errorMessage: String!) {
print("\t AppDelegate \(#function) ++++++++++++++++++ \(errorMessage)")
}
- 使用
imgView = UIImageView(frame: CGRectMake(10, 100, 100, 100))
self.view.addSubview(imgView)
let img = FICDPhoto()
img.sourceImageURL = NSURL(string: "https://img.alicdn.com/bao/uploaded/i2/2836521972/TB2cyksspXXXXanXpXXXXXXXXXX_!!0-paimai.jpg")
FICImageCache.sharedImageCache().retrieveImageForEntity(img, withFormatName: FICDPhotoSquareImage16BitBGRFormatName) { (entity:FICEntity!, fmt:String!, img:UIImage!) in
if entity is FICDPhoto{
self.imgView.image = img
}
}