本篇文章主要介绍了ios如何通过按钮点击异步来加载图片,希望小编整理的相关知识和资料都对你们有所帮助。
比较原始的方法:
方法二:
方法三:
#import#import "StringUtils.h"@interface ImageManager : NSObject{ NSMutableDictionary *_imageDict; NSMutableArray *_imageArr;}@property(nonatomic, strong) NSString *httpUrl;@property(nonatomic, strong) NSMutableDictionary *imageDict;@property(nonatomic, assign) dispatch_queue_t networkQueue;+ (ImageManager *) sharedInstance;- (void)asyncImage:(NSString *)imageName imageView:(UIImageView *)imageView;//插队- (void)asyncImageInsert:(NSString *)imageName imageView:(UIImageView *)imageView insert:(BOOL)insert;//不要在下载之前的数据- (void)asyncImageCleanOld:(NSString *)imageName imageView:(UIImageView *)imageView cleanOld:(BOOL)cleanOld;@end
实现文件:
//// ImageManager.m// myb-ios//// Created by warrior gao on 13-6-5.// Copyright (c) 2013年 51myb. All rights reserved.//#import "ImageManager.h"@interface ImageManager()@end@implementation ImageManager//缓存图片的最大数量static int counter = 0;@synthesize imageDict = _imageDict;//Singleton + (ImageManager *)sharedInstance{ static id instance; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ instance = self.new; }); return instance;}- (id)init{ if((self = [super init])) { self.networkQueue = dispatch_queue_create("com.warrior.network.image", nil); _imageDict = [[NSMutableDictionary alloc] init]; _imageArr = [[NSMutableArray alloc] init]; } return self;}- (NSString *) fileFullPath:(NSString *)fileName{ NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0]; NSString *fileFullPath = [NSString stringWithFormat:@"%@/%@",cachePath,fileName]; return fileFullPath;}//不要在下载之前的数据- (void)asyncImageCleanOld:(NSString *)imageName imageView:(UIImageView *)imageView cleanOld:(BOOL)cleanOld{ if(cleanOld) { [_imageArr removeAllObjects]; } [self asyncImage:imageName imageView:imageView];}//插队,优先- (void)asyncImageInsert:(NSString *)imageName imageView:(UIImageView *)imageView insert:(BOOL)insert{ if([StringUtils isEmpty:imageName]){ return; } NSData *data = [NSData dataWithContentsOfFile:[self fileFullPath:[imageName stringByReplacingOccurrencesOfString:@"/" withString:@"-"]]]; if(data == nil){ [_imageDict setValue:imageView forKey:imageName]; if(insert) { [_imageArr insertObject:imageName atIndex:0]; } else { [_imageArr addObject:imageName]; } [self cacheImage]; } else { [imageView setImage:[UIImage imageWithData:data]]; }}//正常,附加到后面- (void)asyncImage:(NSString *)imageName imageView:(UIImageView *)imageView{ [self asyncImageInsert:imageName imageView:imageView insert:NO];}//异步缓存图片到本地,最多有两个线程-(void)cacheImage{ for (; counter 0; counter++) { NSString *imageName = nil; @synchronized(self){ imageName = [[_imageArr objectAtIndex:0] copy]; [_imageArr removeObjectAtIndex:0]; } if(imageName == nil) continue; dispatch_async(self.networkQueue, ^{ NSLog(@"Starting: %@", imageName); UIImage *avatarImage = nil; NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@",self.httpUrl, imageName]]; NSData *responseData = [NSData dataWithContentsOfURL:url]; if(responseData.length > 0) { [responseData writeToFile:[self fileFullPath:[imageName stringByReplacingOccurrencesOfString:@"/" withString:@"-"]] atomically:NO]; avatarImage = [UIImage imageWithData:responseData]; NSLog(@"Finishing: %@", imageName); if (avatarImage) { dispatch_async(dispatch_get_main_queue(), ^{ UIImageView *imageView = [_imageDict objectForKey:imageName]; if(imageView != nil && avatarImage != nil){ [imageView setImage:avatarImage]; } [_imageDict removeObjectForKey:imageName]; [imageName release]; }); } } counter--; [self cacheImage]; }); }}@end
以上所述就是ios如何通过按钮点击异步来加载图片的介绍,希望大家能够喜欢,更多内容请继续关注武林技术频道网站!
新闻热点
疑难解答