我正在使用UITableView
和CustomCell
。 CustomCell
包含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
中顯示。任何幫助,將不勝感激。
NSLog in - (void)loadImageInBackground:(NSArray *)urlAndTagReference and print [urlAndTagReference objectAtIndex:0] – Durgaprasad
以下是[Lazy Loading](http://tinyurl.com/bh2nahf)的google結果**我認爲你應該選擇一些已經完善的框架來實現這個功能,比如[SDWebImageView](https://github.com/rs/SDWebImage)**。 – Hemang
@Durgaprasad:當我做它的日誌打印圖像的URL,像'http:// www.abcd.com/image/image.png' – Krunal