2013-05-15 40 views
0

我正在使用UITableViewCustomCellCustomCell包含UIImageView和2 UILabel。 在UIImageView我想從加載圖片網址,我想保存在我的文檔目錄中。UITableView中的延遲加載崩潰應用

這裏是我的代碼,我試過了,

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:[URLarr objectAtIndex:indexPath.row]];//arr is NSMutablearray of image URL 

UIImage *img1 = [UIImage imageWithContentsOfFile:savedImagePath]; 
if (!img1 || [UIImagePNGRepresentation(img1) length] <=0) 
{ 
    id path =[URLarr objectAtIndex:indexPath.row]; 
    path = [path stringByReplacingOccurrencesOfString:@" " withString:@"%20"]; 
    NSURL *url = [NSURL URLWithString:path]; 
    NSMutableArray *arr = [[NSMutableArray alloc] initWithObjects:url, [NSString stringWithFormat:@"%d", indexPath.row+1], nil ]; 

    [self performSelectorInBackground:@selector(loadImageInBackground:) withObject:arr]; 

} 

和這一個了。

//—————————————-lazy loading———————– 
- (void)loadImageInBackground:(NSArray *)urlAndTagReference 
{ 
     NSData *imgData = [NSData dataWithContentsOfURL:[urlAndTagReference objectAtIndex:0]]; 
     UIImage *imgload = [[UIImage alloc] initWithData:imgData]; 

     NSMutableArray *arr = [[NSMutableArray alloc] initWithObjects:imgload, [urlAndTagReference objectAtIndex:1], nil]; 

     [self performSelectorOnMainThread:@selector(assignImageToImageView:) withObject:arr waitUntilDone:YES]; 
} 

- (void) assignImageToImageView:(NSArray *)imgAndTagReference 
{ 
     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES); 
     NSString *documentsDirectory = [paths objectAtIndex:0]; 
     NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:[URLarr objectAtIndex:[[imgAndTagReference objectAtIndex:1] intValue]-1]]; 

     UIImage* imageToSave = savedImagePath; 
     NSData *imageData = UIImagePNGRepresentation(imageToSave); 
     [imageData writeToFile:savedImagePath atomically:NO]; 

} 

原因崩潰節目NSInvalidArgumentException。此外,圖片未在UITableViewCell中顯示。任何幫助,將不勝感激。

+0

NSLog in - (void)loadImageInBackground:(NSArray *)urlAndTagReference and print [urlAndTagReference objectAtIndex:0] – Durgaprasad

+0

以下是[Lazy Loading](http://tinyurl.com/bh2nahf)的google結果**我認爲你應該選擇一些已經完善的框架來實現這個功能,比如[SDWebImageView](https://github.com/rs/SDWebImage)**。 – Hemang

+0

@Durgaprasad:當我做它的日誌打印圖像的URL,像'http:// www.abcd.com/image/image.png' – Krunal

回答

0

對於表視圖圖像的延遲加載,可用的選項很少。可以在您的設計中使用它們以節省時間並避免重新發明車輪。
1.蘋果延遲加載代碼 - link
2. SDWebImage - link

乾杯! Amar。

+0

我建議你給予評論鏈接。如果您對所述問題有所瞭解,請提供帶有代碼的良好解釋的答案。 –

0

我覺得

NSData *imgData = [NSData dataWithContentsOfURL:[urlAndTagReference objectAtIndex:0]];

不工作。請嘗試此操作

UIImage *imgload = [UIImage imageWithData:[NSData dataWithContentsOfURL:[urlAndTagReference objectAtIndex:0]]] 

這可能有所幫助。現在試試這個。

UIImageView *imageView = [[UIImageView alloc]init]; 
[imageView_shake_image setImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[urlAndTagReference objectAtIndex:0]]]]; 
UIImage *imgload = [imageView.image retain]; 

我不確定。請試試

+0

gr8,現在它沒有崩潰,但仍然沒有加載圖像.. – Krunal

+0

k它意味着它仍然沒有下載 – Durgaprasad

+0

我編輯檢查 – Durgaprasad