从网上加载图片,当网速慢或是图片较大时,你会发现程序可能会失去对用户的响应.这样你可以用多线程:
解决的方法是从网上down下来一次后就将图片缓存起来,再次显示的时候就不用去下载。
假设有多张图片,用循环存多个路径:
需要写GetImage类,实现刚才的方法.
GetImage.h文件如下:
GetImage.m文件如下:
至此,存储完毕,在用的时候调用刚才存的路径就可以了,可用方法[[UIImage alloc] initWithContentsOfFile:imagePath]
- -(void)
buildData { -
NSOperationQueue *queue = [NSOperationQueue new]; -
-
[queue setMaxConcurrentOperatio nCount:NSOperationQueueDefaultM axConcurrentOperationCou nt]; -
-
NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self -
selector:@selector(downloadImage) -
object:nil]; -
[queue addOperation:operation]; -
[operation release]; - }
-(void) buildData { NSOperationQueue *queue = [NSOperationQueue new]; [queue setMaxConcurrentOperationCount:NSOperationQueueDefaultM axConcurrentOperationCou nt]; NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(downloadImage) object:nil]; [queue addOperation:operation]; [operation release]; }
解决的方法是从网上down下来一次后就将图片缓存起来,再次显示的时候就不用去下载。
假设有多张图片,用循环存多个路径:
- -
(void)downloadImage { -
NSString *imagePath; -
for (...) -
imagePath = [GetImage saveImage:imageUrlPath withCache:@""]; - }
- (void)downloadImage { NSString *imagePath; for (...) imagePath = [GetImage saveImage:imageUrlPath withCache:@""]; }
需要写GetImage类,实现刚才的方法.
GetImage.h文件如下:
- #import
-
-
- @interface
GetImage : NSObject { -
- }
- +(NSString
*) saveImage:(NSString *)urlpath withCache:(NSString *)filename; -
- @end
#import @interface GetImage : NSObject { } +(NSString *) saveImage:(NSString *)urlpath withCache:(NSString *)filename; @end
GetImage.m文件如下:
- @implementation
GetImage - +(NSString
*) saveImage:(NSString *)urlpath withCache:(NSString *)filename - {
-
NSData *retureData=nil; -
NSAutoreleasePool *pool=[[NSAutoreleasePool alloc] init]; -
NSFileManager *fileManager = [NSFileManager defaultManager]; -
NSArray *cache = NSSearchPathForDirectori esInDomains(NSCachesDirectory, NSUserDomainMask, YES); -
NSString *cachePath = [cache objectAtIndex:0] ; -
filename=[filename stringByAppendingFormat:@"%@",[urlpath lastPathComponent]]; -
NSString *filepath = [cachePath stringByAppendingString:@"/"]; -
filepath=[filepath stringByAppendingString:filename]; -
-
NSLog(@"filepath=%@",filepath); -
BOOL success; -
success = [fileManager fileExistsAtPath:filepath]; -
if (success) -
{ -
return filepath; -
-
} -
else -
{ -
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease]; -
[request setURL:[NSURL URLWithString:urlpath]]; -
[request setHTTPMethod:@"GET"]; -
-
NSURLResponse *response; -
NSError *error; -
-
retureData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; -
-
if ([fileManager createDirectoryAtPath:cachePath attributes:nil]==NO){ -
////NSLog(@"fileManager createDirectoryAtPath:cachePath attributes:nil"); -
} -
if ([retureData writeToFile:filepath atomically:YES]){ -
NSLog(@"save Image Success"); -
} -
else -
{ -
NSLog(@"save Image Fail"); -
} -
} -
-
if (retureData !=nil && [fileManager fileExistsAtPath:filepath]){ -
-
return filepath; -
} -
[pool release]; -
-
NSLog(@" Image return nil"); -
return nil; -
-
-
- }
@implementation GetImage +(NSString *) saveImage:(NSString *)urlpath withCache:(NSString *)filename { NSData *retureData=nil; NSAutoreleasePool *pool=[[NSAutoreleasePool alloc] init]; NSFileManager *fileManager = [NSFileManager defaultManager]; NSArray *cache = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); NSString *cachePath = [cache objectAtIndex:0] ; filename=[filename stringByAppendingFormat:@"%@",[urlpath lastPathComponent]]; NSString *filepath = [cachePath stringByAppendingString:@"/"]; filepath=[filepath stringByAppendingString:filename]; NSLog(@"filepath=%@",filepath); BOOL success; success = [fileManager fileExistsAtPath:filepath]; if (success) { return filepath; } else { NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease]; [request setURL:[NSURL URLWithString:urlpath]]; [request setHTTPMethod:@"GET"]; NSURLResponse *response; NSError *error; retureData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; if ([fileManager createDirectoryAtPath:cachePath attributes:nil]==NO){ ////NSLog(@"fileManager createDirectoryAtPath:cachePath attributes:nil"); } if ([retureData writeToFile:filepath atomically:YES]){ NSLog(@"save Image Success"); } else { NSLog(@"save Image Fail"); } } if (retureData !=nil && [fileManager fileExistsAtPath:filepath]){ return filepath; } [pool release]; NSLog(@" Image return nil"); return nil; }
至此,存储完毕,在用的时候调用刚才存的路径就可以了,可用方法[[UIImage alloc] initWithContentsOfFile:imagePath]