0

我想处理下载线程无法成功从 URL 下载图像的情况,以下代码似乎不符合我的目的:

dispatch_queue_t DownloadQueue = dispatch_queue_create("Download Pic", NULL);
dispatch_async(DownloadQueue, ^{
    NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:detailImageURL]];
    dispatch_async(dispatch_get_main_queue(), ^{
        if ([imageData isKindOfClass:[NSNull class]]) {
            detailImageView.image = [UIImage imageNamed:@"no_image_available.jpg"];

        }
        else{
            UIImage *image = [UIImage imageWithData:imageData];
            detailImageView.image = image;
        }
    });
});
dispatch_release(DownloadQueue);
4

1 回答 1

0

@Rob,thanx,解决问题的解决方法:

dispatch_queue_t DownloadQueue = dispatch_queue_create("Download Pic", NULL);
dispatch_async(DownloadQueue, ^{
    NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:detailImageURL]];
    if (imageData == nil) {
        detailImageView.image = [UIImage imageNamed:@"no_image_available.jpg"];

    }else{
        dispatch_async(dispatch_get_main_queue(), ^{
            UIImage *image = [UIImage imageWithData:imageData];
            detailImageView.image = image;
    });
    }
});
dispatch_release(DownloadQueue);

在进入async块之前,测试是否NSData为 nil。

于 2013-06-12T18:41:55.930 回答