这是我在StackOver流程中的第一篇文章,我的英语不好。我是iOS的初学者,所以我很慢一点。 实际上我想从URL解析我的5个数据(包括Image)。我使用自定义tableView单元格。我的纯文本显示在自定义单元格(标签中),但图像不是。这就是我使用“dispatch_queue_t”在特定的ImageView中加载图像的原因。当我运行模拟器时,图像显示(Logo& BackGround)一个接一行完美,但问题发生在我重置模拟器时。重置后,每个单元格中加载一个图像(队列中的最后一个图像)(总共5个)。我在“viewDidLoad”中调用了我的“parsingLoad”方法,因此它只在加载viewController时加载一次。任何人都可以告诉我,我错了什么?
这是我的代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"FairListCustomCell";
FairListCustomCell *cell = (FairListCustomCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
NSArray *topLabelObject = [[NSBundle mainBundle] loadNibNamed:@"FairListCustomCell" owner:self options:nil];
for (id currentObject in topLabelObject)
{
if ([currentObject isKindOfClass:[UITableViewCell class]])
{
cell = (FairListCustomCell*) currentObject;
break;
}
}
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
currentFair = [fairs objectAtIndex:indexPath.row];
cell.fairNameLabel.text =currentFair.FairName;
cell.fairLocationLabel.text =currentFair.FairLocation;
cell.bookmarkImageView.image=[UIImage imageNamed:@"Favorite.png"];
cell.bookmarkImageView.tag=indexPath.row;
dispatch_queue_t imageQueueFairLogo = dispatch_queue_create("com.queue", NULL);
dispatch_async(imageQueueFairLogo, ^
{
dispatch_retain(imageQueueFairLogo);
UIImage *imageFairLogo = [UIImage imageWithData:[NSData dataWithContentsOfURL: [NSURL URLWithString:[currentFair FairLogo]]]];
dispatch_async(dispatch_get_main_queue(), ^
{
cell.fairLogoImageView.image = imageFairLogo;
#if NEEDS_DISPATCH_RETAIN_RELEASE
dispatch_release(imageQueueFairLogo);
#endif
});
});
dispatch_queue_t imageQueueFairCellBackGround = dispatch_queue_create("com.queue", NULL);
dispatch_async(imageQueueFairCellBackGround, ^
{
dispatch_retain(imageQueueFairCellBackGround);
UIImage *imageFairCellBackGround = [UIImage imageWithData:[NSData dataWithContentsOfURL: [NSURL URLWithString:[currentFair FairCellBackGround]]]];
dispatch_async(dispatch_get_main_queue(), ^
{
cell.fairCellBackGround.image = imageFairCellBackGround;
#if NEEDS_DISPATCH_RETAIN_RELEASE
dispatch_release(imageQueueFairCellBackGround);
#endif
});
});
return cell;
}
我也尝试了另外两种方式。 第一: - 对两个图像使用“global_queue”(这里只是放一个样本), 像这样:
imageQueueFairCellBackGround = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(imageQueueFairCellBackGround, ^
{
dispatch_retain(imageQueueFairCellBackGround);
UIImage *imageFairCellBackGround = [UIImage imageWithData:[NSData dataWithContentsOfURL: [NSURL URLWithString:[currentFair FairCellBackGround]]]];
dispatch_async(dispatch_get_main_queue(), ^
{
cell.fairCellBackGround.image = imageFairCellBackGround;
#if NEEDS_DISPATCH_RETAIN_RELEASE
dispatch_release(imageQueueFairCellBackGround);
#endif
});
});
return cell;
第二名: - 像这样:
dispatch_queue_t fairCellBackGroundcallerQueue = dispatch_get_current_queue();
dispatch_queue_t fairCellBackGrounddownloadQueue = dispatch_queue_create("Thumb downloader", NULL);
dispatch_async(fairCellBackGrounddownloadQueue, ^
{
UIImage *imageFairCellBackGround = [UIImage imageWithData:[NSData dataWithContentsOfURL: [NSURL URLWithString:[currentFair FairCellBackGround]]]];
dispatch_async(fairCellBackGroundcallerQueue, ^
{
cell.fairCellBackGround.image = imageFairCellBackGround;
});
});
dispatch_release(fairCellBackGrounddownloadQueue);
但仍然没有解决。 如果有任何人有任何解决方案,请与我分享。 非常感谢,先进。
答案 0 :(得分:4)
我找到了这个特殊问题的解决方案。 这是:
imageQueueFairLogo = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(imageQueueFairLogo, ^
{
UIImage *imageFairLogo = [UIImage imageWithData:[NSData dataWithContentsOfURL: [NSURL URLWithString:[currentFair FairLogo]]]];
dispatch_async(dispatch_get_main_queue(), ^
{
//[imageCache setObject:imageLogoCache forKey:currentFair.FairLogo];
cell.fairLogoImageView.image = imageFairLogo;
[cell setNeedsLayout];
});
});
当然“宣布”& “ @synthesize ”“MyClass.h”中相应的调度队列& “MyClass.m”文件是这样的:
@property(nonatomic) dispatch_queue_t imageQueueFairLogo;
@synthesize imageQueueFairLogo;
为“imageQueueFairCellBackGround”做同样的事情 谢谢。 :)
答案 1 :(得分:2)
试试这个
if (postAndCheckInDetails.postImageURL.length != 0)
{
if (!uploadImage)
uploadImage = (UIImageView*)[cell viewWithTag:4];
dispatch_queue_t checkInQueueForPostImage = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(checkInQueueForPostImage, ^{
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:postAndCheckInDetails.postImageURL]]];
dispatch_sync(dispatch_get_main_queue(), ^{
if (image!=nil) {
[uploadImage setImage:image];
[uploadImage setFrame:CGRectMake(80, checkInDateAndTime.frame.origin.y+40, 160, 120)];
}
[cell setNeedsLayout];
});
});
}
答案 2 :(得分:0)
使用dispatch_retain和dispatch_release注释掉这些行,并在更新图像后添加[cell setNeedsDisplay]