iPhone程序中圖片延時加載

轉自:http://wonderzl.iteye.com/blog/696160

從網上加載圖片,當網速慢或是圖片較大時,你會發現程序可能會失去對用戶的響應.這樣你可以用多線程: 


Java代碼  收藏代碼
  1. -(void) buildData {  
  2.     NSOperationQueue *queue = [NSOperationQueue new];  
  3.       
  4.     [queue setMaxConcurrentOperationCount:NSOperationQueueDefaultMaxConcurrentOperationCount];  
  5.       
  6.     NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self   
  7.                                                                             selector:@selector(downloadImage)   
  8.                                                                               object:nil];  
  9.     [queue addOperation:operation];  
  10.     [operation release];  
  11. }  



解決的方法是從網上down下來一次後就將圖片緩存起來,再次顯示的時候就不用去下載。 
假設有多張圖片,用循環存多個路徑: 
Java代碼  收藏代碼
  1. - (void)downloadImage {  
  2.     NSString *imagePath;  
  3.     for (...)  
  4.         imagePath = [GetImage saveImage:imageUrlPath withCache:@""];  
  5. }  

需要寫GetImage類,實現剛纔的方法. 
GetImage.h文件如下: 
Java代碼  收藏代碼
  1. #import <Foundation/Foundation.h>  
  2.   
  3.   
  4. @interface GetImage : NSObject {  
  5.       
  6. }  
  7. +(NSString *) saveImage:(NSString *)urlpath withCache:(NSString *)filename;  
  8.   
  9. @end  


GetImage.m文件如下: 
Java代碼  收藏代碼
  1. @implementation GetImage  
  2. +(NSString *) saveImage:(NSString *)urlpath withCache:(NSString *)filename  
  3. {  
  4.     NSData *retureData=nil;  
  5.     NSAutoreleasePool *pool=[[NSAutoreleasePool alloc] init];  
  6.     NSFileManager *fileManager = [NSFileManager defaultManager];  
  7.     NSArray *cache = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);  
  8.     NSString *cachePath = [cache objectAtIndex:0] ;   
  9.     filename=[filename stringByAppendingFormat:@"%@",[urlpath lastPathComponent]];  
  10.     NSString *filepath = [cachePath stringByAppendingString:@"/"];            
  11.     filepath=[filepath stringByAppendingString:filename];  
  12.       
  13.     NSLog(@"filepath=%@",filepath);  
  14.     BOOL success;  
  15.     success = [fileManager fileExistsAtPath:filepath];  
  16.     if (success)   
  17.     {  
  18.         return   filepath;  
  19.           
  20.     }  
  21.     else  
  22.     {  
  23.         NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];    
  24.         [request setURL:[NSURL URLWithString:urlpath]];    
  25.         [request setHTTPMethod:@"GET"];    
  26.           
  27.         NSURLResponse *response;  
  28.         NSError *error;  
  29.           
  30.         retureData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];  
  31.           
  32.         if ([fileManager createDirectoryAtPath:cachePath attributes:nil]==NO){  
  33.             ////NSLog(@"fileManager createDirectoryAtPath:cachePath attributes:nil");  
  34.         }  
  35.         if ([retureData writeToFile:filepath atomically:YES]){  
  36.             NSLog(@"save Image Success");  
  37.         }  
  38.         else  
  39.         {  
  40.             NSLog(@"save Image Fail");  
  41.         }  
  42.     }  
  43.       
  44.     if (retureData !=nil && [fileManager fileExistsAtPath:filepath]){  
  45.           
  46.         return   filepath;  
  47.     }  
  48.     [pool release];  
  49.       
  50.     NSLog(@" Image return nil");  
  51.     return nil;  
  52.       
  53.       
  54.       
  55. }  


至此,存儲完畢,在用的時候調用剛纔存的路徑就可以了,可用方法[[UIImage alloc] initWithContentsOfFile:imagePath] 
發佈了19 篇原創文章 · 獲贊 15 · 訪問量 37萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章