如果图片未从网址加载,如何将local image.png添加到单元格?
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"Cell";
DetailCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
NSURL* url = [NSURL URLWithString:[arrayOfImages objectAtIndex:indexPath.row]];
NSURLRequest* request = [NSURLRequest requestWithURL:url];
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse * response,
NSData * imagedata,
NSError * error) {
if (!error){
UIImage* image = [[UIImage alloc] initWithData:imagedata];
cell.flowerImage.image = image;
}
}];
return cell;
}
arrayOfImages
- 我从json解析的url字符串数组。
答案 0 :(得分:1)
您可以针对nil检查图像以验证图像网址。
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse * response,
NSData * imagedata,
NSError * error) {
if (!error){
UIImage* image = [[UIImage alloc] initWithData:imagedata];
if(!image) {
cell.flowerImage.image = [UIImage imageNamed:@"localImage"]; //local bundle image.
} else
cell.flowerImage.image = image;
} else {
cell.flowerImage.image = [UIImage imageNamed:@"localImage"];//local bundle image.
}
}];