我尝试UIImageView
使用新的 'loadImageNamed:' 方法来扩展。在此方法中,我使用了一个dispatchQueue
但XCode
会引发错误,即在“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