无限互联SDWebImage图片缓存流程分析.doc

上传人:文库蛋蛋多 文档编号:2388730 上传时间:2023-02-17 格式:DOC 页数:5 大小:53.50KB
返回 下载 相关 举报
无限互联SDWebImage图片缓存流程分析.doc_第1页
第1页 / 共5页
无限互联SDWebImage图片缓存流程分析.doc_第2页
第2页 / 共5页
无限互联SDWebImage图片缓存流程分析.doc_第3页
第3页 / 共5页
无限互联SDWebImage图片缓存流程分析.doc_第4页
第4页 / 共5页
无限互联SDWebImage图片缓存流程分析.doc_第5页
第5页 / 共5页
亲,该文档总共5页,全部预览完了,如果喜欢就下载吧!
资源描述

《无限互联SDWebImage图片缓存流程分析.doc》由会员分享,可在线阅读,更多相关《无限互联SDWebImage图片缓存流程分析.doc(5页珍藏版)》请在三一办公上搜索。

1、【无限互联】SDWebImage图片缓存流程分析SDWebImage是一个功能很强大的缓存网络图片的框架。框架实现了异步加载网络图片、自动缓存图片数据等功能。以UIImageView加载网络图片为例,对其总体的加载图片流程做一个大致的分析。 首先使用SDWebImage先要导入 #import UIImageView+WebCache.h文件,设置网络图片的图片地址就可以加载图片。objc view plaincopyprint?1. NSURL *url = NSURL URLWithString: 2. imageView setImageWithURL:url; 根据网络图片地址,SDW

2、ebImageManager类提供了方法downloadWithURL加载图片objc view plaincopyprint?1. id operation = SDWebImageManager.sharedManager downloadWithURL:url options:options progress:progressBlock completed:(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished) 2. if (!wself) return; 3. dispatch_main_s

3、ync_safe( 4. if (!wself) return; 5. if (image) 6. wself.image = image; 7. wself setNeedsLayout; 8. 9. if (completedBlock & finished) 10. completedBlock(image, error, cacheType); 11. 12. ); 13. ; 14. objc_setAssociatedObject(self, &operationKey, operation, OBJC_ASSOCIATION_RETAIN_NONATOMIC); 15. id o

4、peration = SDWebImageManager.sharedManager downloadWithURL:url options:options progress:progressBlock completed:(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished) if (!wself) return; dispatch_main_sync_safe( if (!wself) return; if (image) wself.image = image; wself setNeedsL

5、ayout; if (completedBlock & finished) completedBlock(image, error, cacheType); ); ; objc_setAssociatedObject(self, &operationKey, operation, OBJC_ASSOCIATION_RETAIN_NONATOMIC); 进入downloadWithURL方法,由SDImagerCache类提供方法queryDiskCacheForKey检查内存中是否有图片缓存,如果有图片缓存,回调block将图片数据传递给SDWebImageManger类。SDWebImage

6、Mange类回调block将图片数据传递给UIImageView+WebCache类去显示图片。objc view plaincopyprint?1. UIImage *image = self imageFromMemoryCacheForKey:key; 2. if (image) 3. doneBlock(image, SDImageCacheTypeMemory); 4. return nil; 5. UIImage *image = self imageFromMemoryCacheForKey:key; if (image) doneBlock(image, SDImageCach

7、eTypeMemory); return nil; 如果内存中没有图片缓存,将会去磁盘中查找图片缓存。这一步操作是异步操作,如果从磁盘中读取到图片数据,将图片数据添加到内存缓存中,回调block将图片数据传递给SDWebImageManger类。 SDWebImageMange类回调block将图片数据传递给UIImageView+WebCache类去显示图片objc view plaincopyprint?1. dispatch_async(self.ioQueue, 2. if (operation.isCancelled) 3. return; 4. 5.6. autoreleasepo

8、ol 7. UIImage *diskImage = self diskImageForKey:key; 8. if (diskImage) 9. CGFloat cost = diskImage.size.height * diskImage.size.width * diskImage.scale; 10. self.memCache setObject:diskImage forKey:key cost:cost; 11. 12.13. dispatch_async(dispatch_get_main_queue(), 14. doneBlock(diskImage, SDImageCa

9、cheTypeDisk); 15. ); 16. dispatch_async(self.ioQueue, if (operation.isCancelled) return; autoreleasepool UIImage *diskImage = self diskImageForKey:key; if (diskImage) CGFloat cost = diskImage.size.height * diskImage.size.width * diskImage.scale; self.memCache setObject:diskImage forKey:key cost:cost

10、; dispatch_async(dispatch_get_main_queue(), doneBlock(diskImage, SDImageCacheTypeDisk); ); 内存和磁盘中都没有图片缓存,由SDWebImageDownloader类downloadImageWithURL方法请求网络下载图片。objc view plaincopyprint?1. - (id )downloadImageWithURL:(NSURL *)url options:(SDWebImageDownloaderOptions)options progress:(void ()(NSInteger,

11、 NSInteger)progressBlock completed:(void ()(UIImage *, NSData *, NSError *, BOOL)completedBlock 2. - (id )downloadImageWithURL:(NSURL *)url options:(SDWebImageDownloaderOptions)options progress:(void ()(NSInteger, NSInteger)progressBlock completed:(void ()(UIImage *, NSData *, NSError *, BOOL)comple

12、tedBlock 图片下载完成后,数据下载完成后交给 SDWebImageDecoder 做图片解码处理,图片解码完成后回调给SDWebImageDownloader类,SDWebImageDownloader类,回调block将图片数据传递给SDWebImageManger类。 SDWebImageMange类回调block将图片数据传递给UIImageView+WebCache类显示图片。图片数据会写入到内存和磁盘缓存中。图片数据写入磁盘缓存中是异步操作,避免拖慢主线程。objc view plaincopyprint?1. - (void)storeImage:(UIImage *)im

13、age recalculateFromImage:(BOOL)recalculate imageData:(NSData *)imageData forKey:(NSString *)key toDisk:(BOOL)toDisk 2. if (!image | !key) 3. return; 4. 5. self.memCache setObject:image forKey:key cost:image.size.height * image.size.width * image.scale; 6. if (toDisk) 7. dispatch_async(self.ioQueue,

14、8. NSData *data = imageData; 9.10. if (image & (recalculate | !data) 11. BOOL imageIsPng = YES; 12. if (imageData length = kPNGSignatureData length) 13. imageIsPng = ImageDataHasPNGPreffix(imageData); 14. 15. if (imageIsPng) 16. data = UIImagePNGRepresentation(image); 17. 18. else 19. data = UIImage

15、JPEGRepresentation(image, (CGFloat)1.0); 20. 21. data = NSBitmapImageRep representationOfImageRepsInArray:image.representations usingType: NSJPEGFileType properties:nil; 22. 23. if (data) 24. if (!_fileManager fileExistsAtPath:_diskCachePath) 25. _fileManager createDirectoryAtPath:_diskCachePath wit

16、hIntermediateDirectories:YES attributes:nil error:NULL; 26. 27. _fileManager createFileAtPath:self defaultCachePathForKey:key contents:data attributes:nil; 28. 29. ); 30. 31. - (void)storeImage:(UIImage *)image recalculateFromImage:(BOOL)recalculate imageData:(NSData *)imageData forKey:(NSString *)k

17、ey toDisk:(BOOL)toDisk if (!image | !key) return; self.memCache setObject:image forKey:key cost:image.size.height * image.size.width * image.scale; if (toDisk) dispatch_async(self.ioQueue, NSData *data = imageData; if (image & (recalculate | !data) BOOL imageIsPng = YES; if (imageData length = kPNGS

18、ignatureData length) imageIsPng = ImageDataHasPNGPreffix(imageData); if (imageIsPng) data = UIImagePNGRepresentation(image); else data = UIImageJPEGRepresentation(image, (CGFloat)1.0); data = NSBitmapImageRep representationOfImageRepsInArray:image.representations usingType: NSJPEGFileType properties

19、:nil; if (data) if (!_fileManager fileExistsAtPath:_diskCachePath) _fileManager createDirectoryAtPath:_diskCachePath withIntermediateDirectories:YES attributes:nil error:NULL; _fileManager createFileAtPath:self defaultCachePathForKey:key contents:data attributes:nil; ); 总结:1、SDWebImageManager *manager = SDWebImageManager sharedManager;管理图片的加载,由SDImagerCache类检查内存中是否有图片缓存,或者由SDWebImageDownloader类请求网络下载图片。2、SDImageCache 类管理图片缓存,读取图片缓存和写入图片缓存。3、SDWebImageDownloader,根据URL向网络读取数据。

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 建筑/施工/环境 > 项目建议


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号