1

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

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

4

3 回答 3

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];
        });
    });

当然,在“MyClass.h”和“MyClass.m”文件中“声明”和“ @synthesize ”相应的调度队列,如下所示:

    @property(nonatomic) dispatch_queue_t imageQueueFairLogo;
    @synthesize imageQueueFairLogo;

为“imageQueueFairCellBackGround”做同样的事情谢谢。:)

于 2013-07-29T20:00:11.543 回答
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];
                        });
                    });
                }
于 2013-07-03T11:11:14.977 回答
0

使用 dispatch_retain 和 dispatch_release 注释掉这些行,并在更新图像后添加 [cell setNeedsDisplay]

于 2013-07-03T11:15:05.880 回答