0

我有一个UITableView显示带有图像的单元格。在cellForRowAtIndexPath我启动连接以获取图像的方法上。我想跟踪已经请求了哪些图像,以避免多次请求相同的图像。

我设置了这样的方法

+(void) setImageFromURL:(NSURL*) url fromImage:(UIImage*) srcImage to:(UIImageView*) dstImage wasRequested:(BOOL*) requested;

并这样称呼它

[Utils setImageFromURL:event.thumbURL fromImage:event.thumbImage to:cell.image wasRequested:event.thumbRequested];

其中 event.thumbRequested 是一个 BOOL 属性。该方法看起来像这样

+(void) setImageFromURL:(NSURL*) url fromImage:(UIImage*) srcImage to:(UIImageView*) dstImage wasRequested:(BOOL*) requested{
    if (srcImage != nil) {
        dstImage.image = srcImage;
    } else if (!requested) {
        requested = YES;
        @try {
            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                NSData * data = [[NSData alloc] initWithContentsOfURL: url];
                if ( data == nil )
                    return;
                dispatch_async(dispatch_get_main_queue(), ^{
                    __block UIImage* srcImage = [UIImage imageWithData: data];
                    dstImage.image = srcImage;
                });
                [data release];
            });
        }
        @catch (NSException *exception) {
            NSLog(exception.debugDescription);
        }
    }
}

我的问题是BOOL由于在Objective-C. 谁能推荐一种能够BOOL从方法内部更新事件对象的方法?

4

1 回答 1

6

该行:

requested = YES;

需要是:

if (requested) {
    *requested = YES;
}

支票只是以防nil万一requested

编辑:电话应该是:

BOOL requested = event.thumbRequested;
[Utils setImageFromURL:event.thumbURL fromImage:event.thumbImage to:cell.image wasRequested:&requested];
event.thumbRequested = requested;
于 2012-10-31T05:30:47.673 回答