gpt4 book ai didi

iphone - 使用 "dispatch_queue_t"解析自定义表格单元格中的图像无法正常工作

转载 作者:行者123 更新时间:2023-12-01 17:41:34 26 4
gpt4 key购买 nike

这是我在 StackOver flow 中的第 1 篇文章,我的英语不好。我是iOS的初学者,所以我有点慢。
实际上我想从 URL 解析我的 5 个数据(包括图像)。我使用自定义 tableView 单元格。我的纯文本清晰地显示在自定义单元格(标签中)中,但图像没有。这就是为什么我使用“dispatch_queue_t”在特定的 ImageView 中加载图像的原因。当我运行模拟器时,图像( Logo 和背景)一排排完美地显示出来,但是当我重置模拟器时会出现问题。重置后,每个单元格(共 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);

但仍然没有解决。
如果有人有任何解决方案,请与我分享。
非常感谢,提前。

最佳答案

我找到了这个特定问题的解决方案。
这里是:

    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”做同样的事情
谢谢。 :)

关于iphone - 使用 "dispatch_queue_t"解析自定义表格单元格中的图像无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17446109/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com