4

我尝试UIImageView使用新的 'loadImageNamed:' 方法来扩展。在此方法中,我使用了一个dispatchQueueXCode会引发错误,即在“UIImageView”类型的对象上找不到属性“dispatchQueue”。

有人可以给我一个提示吗?!谢谢!

#import "UIImageView+LazyPicLoad.h"

@implementation UIImageView (LazyPicLoad)

-(void)loadImageNamed:(NSString *)name {
dispatch_async(self.dispatchQueue, ^{
    NSData *imageData;
    if (name) {
        if ([UIScreen mainScreen].scale == 2.0)
            imageData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[name stringByAppendingString:@"@2x"] ofType:@"png"]];
        else
            imageData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:name ofType:@"png"]];
    }

    CGImageRef decompressedImage = NULL;
    if (imageData) {
        CGDataProviderRef imageDataProvider = CGDataProviderCreateWithCFData((__bridge CFDataRef)imageData);
        CGImageRef image = CGImageCreateWithPNGDataProvider(imageDataProvider, NULL, NO, kCGRenderingIntentDefault);
        CGContextRef bitmapContext = CGBitmapContextCreate(NULL, CGImageGetWidth(image), CGImageGetHeight(image), CGImageGetBitsPerComponent(image), CGImageGetWidth(image) * 4, CGColorSpaceCreateDeviceRGB(), kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Little);
        CGContextDrawImage(bitmapContext, CGRectMake(0, 0, CGImageGetWidth(image), CGImageGetHeight(image)), image);
        decompressedImage = CGBitmapContextCreateImage(bitmapContext);

        CGDataProviderRelease(imageDataProvider);
        CGImageRelease(image);
        CGContextRelease(bitmapContext);
    }

    dispatch_sync(dispatch_get_main_queue(), ^{
        if (decompressedImage)
            self.image = [UIImage imageWithCGImage:decompressedImage];
    });

    if (decompressedImage)
        CGImageRelease(decompressedImage);
});
}

@end
4

1 回答 1

1

这正是问题所在,UIImageView 上没有 dispatchQueue 属性。如果你想将该块发送到后台,你应该使用它来代替:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    // background code
});
于 2012-12-21T13:14:19.213 回答