2011-06-24 44 views
0

我使用Apple的LazyTableImages示例異步下載圖像爲UITableViewCells。主要區別在於我將文件的副本保存到「文檔」文件夾,因此無需一直下載它。下面的方法是由負責下載圖像異步圖像下載UITableView的cellForRowAtIndexPath並不總是調用

- (void)imageDidLoad:(NSIndexPath *)indexPath 
{ 
    AsyncImage *aImage = [imageDownloadsInProgress objectForKey:indexPath]; 
    if (aImage != nil) 
    { 
     UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:aImage.indexPathInTableView]; 

     cell.imageView.image = aImage.image; 
    } 
} 

當圖像不在設備上的類調用,下載是罰款和顯示圖像。但是,當圖像在設備上時,圖像不顯示,單元變量爲空。

的AsyncImage的代碼是:

// 
// AsyncImgView.m 
// BelfastChamberOfCommerce 
// 
// Created by markpirvine on 23/06/2011. 
// Copyright 2011 __MyCompanyName__. All rights reserved. 
// 

#import "AsyncImage.h" 
#import "AppManager.h" 

#define kAppIconHeight 48 

@interface AsyncImage() 
@property (nonatomic, retain) NSString *fileUrl; 
@end 


@implementation AsyncImage 

@synthesize imageUrl, fileUrl; 
@synthesize image; 
@synthesize indexPathInTableView; 
@synthesize delegate; 

- (void)dealloc 
{ 
    [imageUrl release]; 
    [fileUrl release]; 
    [image release]; 
    [indexPathInTableView release]; 
    [connection cancel]; 
    [connection release]; 
    [data release]; 
    [super dealloc]; 
} 

- (void)loadImage 
{ 
    if (connection != nil) 
    { 
     [connection release]; 
    } 

    if (data != nil) 
    { 
     [data release]; 
    } 

    if([self.imageUrl length] > 0) 
    { 
     self.fileUrl = [[[AppManager sharedInstance] getCacheLocation] stringByAppendingPathComponent:[[self.imageUrl componentsSeparatedByString:@"/"] lastObject]]; 

     if([[NSFileManager defaultManager] fileExistsAtPath:self.fileUrl] == YES) 
     { 
      self.image = [UIImage imageWithContentsOfFile:self.fileUrl]; 

      [self deliverImage]; 
     } 
     else 
     { 
      NSURLRequest* request = [NSURLRequest requestWithURL:[NSURL URLWithString:self.imageUrl] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0]; 
      connection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; 
     } 
    } 
} 

- (void)connection:(NSURLConnection *)theConnection didReceiveData:(NSData *)incrementalData 
{ 
    if (data == nil) 
    { 
     data = [[NSMutableData alloc] initWithCapacity:2048]; 
    } 

    [data appendData:incrementalData]; 
} 

- (void)connectionDidFinishLoading:(NSURLConnection*)theConnection 
{ 
    [connection release]; 
    connection = nil; 

    UIImage *iTmp = [[UIImage alloc] initWithData:data]; 

    if (iTmp.size.width != kAppIconHeight && iTmp.size.height != kAppIconHeight) 
    { 
     CGSize itemSize = CGSizeMake(kAppIconHeight, kAppIconHeight); 
     UIGraphicsBeginImageContext(itemSize); 
     CGRect imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height); 
     [iTmp drawInRect:imageRect]; 
     self.image = UIGraphicsGetImageFromCurrentImageContext(); 
     UIGraphicsEndImageContext(); 
    } 
    else 
    { 
     self.image = iTmp; 
    } 

    [UIImageJPEGRepresentation(self.image, 1.0) writeToFile:self.fileUrl atomically:YES]; 

    [iTmp release]; 

    [data release]; 
    data = nil; 

    [self deliverImage]; 
} 

- (void)deliverImage 
{ 
    [delegate imageDidLoad:self.indexPathInTableView]; 
} 

- (void)cancelDownload 
{ 
    [connection cancel]; 
    connection = nil; 
    data = nil; 
} 

@end 

任何幫助,將不勝感激

回答