我已经从here复制了文件的内容并创建了一个UIImageView + AFNetworking.h并导入了我的实现文件
现在,当我编写以下代码时,我收到此错误,但是当我删除代码块时,一切正常。
我想在自定义表格单元格中显示图像。我正在通过JSON抓取图像的网址
NSURLRequest *imageRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:[dictArray objectForKey:@"image"]]];
[cell.thumbnailImageView setImageWithURLRequest:imageRequest placeholderImage:nil
success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image){
NSLog(@"success");
cell.thumbnailImageView.image = image;
cell.thumbnailImageView.contentMode = UIViewContentModeScaleAspectFit;
cell.thumbnailImageView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[cell setNeedsLayout];// To update the cell {if not using this, your image is not showing over cell.}
}failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error){
NSLog(@"Failure");}];
以下是错误的屏幕截图
加载后崩溃
答案 0 :(得分:1)
而不是使用afnetworking你可以使用调度方法
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^{
NSString *url = [indexDic objectForKey:@"image"];
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:url]];
UIImage *image = [UIImage imageWithData:imageData];
dispatch_async(dispatch_get_main_queue(), ^{
cellImg.image = image;
});
});
return cell;
}