The UIImageView extension with categories causes an error on self.dispatchQueue

I am trying to extend the UIImageView with the new loadImageNamed: method. Inside this method, I use dispatchQueue , but XCode throws an error that the property "dispatchQueue" was not found in an Object of type "UIImageView".

Can someone give me a hint ?! Thanks!

 #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
source share
1 answer

It is in this problem that there is no dispatchQueue property in the UIImageView. If you want to send this block to the background, you should use this instead:

 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ // background code }); 
+1
source

All Articles