mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-15 19:26:42 +08:00
perf(ios): autoreleasepool for image extensions
This commit is contained in:
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -34,7 +34,7 @@
|
|||||||
</data>
|
</data>
|
||||||
<key>Info.plist</key>
|
<key>Info.plist</key>
|
||||||
<data>
|
<data>
|
||||||
WsLLFjQ3sruW6pvEfLRTXorurcg=
|
xwF1juLfCl48xVJYOqCb8tG0WhQ=
|
||||||
</data>
|
</data>
|
||||||
<key>Modules/module.modulemap</key>
|
<key>Modules/module.modulemap</key>
|
||||||
<data>
|
<data>
|
||||||
|
Binary file not shown.
@ -12,65 +12,65 @@
|
|||||||
@implementation UIImage (TNSBlocks)
|
@implementation UIImage (TNSBlocks)
|
||||||
|
|
||||||
static dispatch_queue_t image_queue;
|
static dispatch_queue_t image_queue;
|
||||||
static NSLock* image_lock_handle;
|
|
||||||
|
|
||||||
+ (void) initialize {
|
+ (void) initialize {
|
||||||
image_queue = dispatch_queue_create("org.nativescript.TNSWidgets.image", NULL);
|
image_queue = dispatch_queue_create("org.nativescript.TNSWidgets.image", NULL);
|
||||||
if ([[NSProcessInfo processInfo] operatingSystemVersion].majorVersion >= 9) {
|
|
||||||
// UIImage imageNamed: is said to be thread safe, in iOS9 and later, in offical Apple reference.
|
|
||||||
image_lock_handle = nil;
|
|
||||||
} else {
|
|
||||||
image_lock_handle = [NSLock new];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void) tns_forceDecode {
|
- (void) tns_forceDecode {
|
||||||
|
@autoreleasepool {
|
||||||
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
|
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
|
||||||
CGContextRef context = CGBitmapContextCreate(NULL, 1, 1, 8, 0, colorSpace, kCGImageAlphaPremultipliedFirst);
|
CGContextRef context = CGBitmapContextCreate(NULL, 1, 1, 8, 0, colorSpace, kCGImageAlphaPremultipliedFirst);
|
||||||
CGContextDrawImage(context, CGRectMake(0, 0, 1, 1), [self CGImage]);
|
CGContextDrawImage(context, CGRectMake(0, 0, 1, 1), [self CGImage]);
|
||||||
CGContextRelease(context);
|
CGContextRelease(context);
|
||||||
CGColorSpaceRelease(colorSpace);
|
CGColorSpaceRelease(colorSpace);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
+ (void) tns_safeDecodeImageNamed: (NSString*) name completion: (void (^) (UIImage*))callback {
|
+ (void) tns_safeDecodeImageNamed: (NSString*) name completion: (void (^) (UIImage*))callback {
|
||||||
dispatch_async(image_queue, ^(void){
|
dispatch_async(image_queue, ^(void){
|
||||||
[image_lock_handle lock];
|
@autoreleasepool {
|
||||||
UIImage* image = [UIImage imageNamed: name];
|
UIImage* image = [UIImage imageNamed: name];
|
||||||
[image_lock_handle unlock];
|
|
||||||
[image tns_forceDecode];
|
[image tns_forceDecode];
|
||||||
|
|
||||||
dispatch_async(dispatch_get_main_queue(), ^(void) {
|
dispatch_async(dispatch_get_main_queue(), ^(void) {
|
||||||
callback(image);
|
callback(image);
|
||||||
});
|
});
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
+ (UIImage*) tns_safeImageNamed: (NSString*) name {
|
+ (UIImage*) tns_safeImageNamed: (NSString*) name {
|
||||||
[image_lock_handle lock];
|
UIImage* image;
|
||||||
UIImage* image = [UIImage imageNamed: name];
|
@autoreleasepool {
|
||||||
[image_lock_handle unlock];
|
image = [UIImage imageNamed: name];
|
||||||
|
}
|
||||||
return image;
|
return image;
|
||||||
}
|
}
|
||||||
|
|
||||||
+ (void) tns_decodeImageWithData: (NSData*) data completion: (void (^) (UIImage*))callback {
|
+ (void) tns_decodeImageWithData: (NSData*) data completion: (void (^) (UIImage*))callback {
|
||||||
dispatch_async(image_queue, ^(void) {
|
dispatch_async(image_queue, ^(void) {
|
||||||
|
@autoreleasepool {
|
||||||
UIImage* image = [UIImage imageWithData: data];
|
UIImage* image = [UIImage imageWithData: data];
|
||||||
[image tns_forceDecode];
|
[image tns_forceDecode];
|
||||||
|
|
||||||
dispatch_async(dispatch_get_main_queue(), ^(void) {
|
dispatch_async(dispatch_get_main_queue(), ^(void) {
|
||||||
callback(image);
|
callback(image);
|
||||||
});
|
});
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
+ (void) tns_decodeImageWidthContentsOfFile: (NSString*) file completion: (void (^) (UIImage*))callback {
|
+ (void) tns_decodeImageWidthContentsOfFile: (NSString*) file completion: (void (^) (UIImage*))callback {
|
||||||
dispatch_async(image_queue, ^(void) {
|
dispatch_async(image_queue, ^(void) {
|
||||||
|
@autoreleasepool {
|
||||||
UIImage* image = [UIImage imageWithContentsOfFile: file];
|
UIImage* image = [UIImage imageWithContentsOfFile: file];
|
||||||
[image tns_forceDecode];
|
[image tns_forceDecode];
|
||||||
|
|
||||||
dispatch_async(dispatch_get_main_queue(), ^(void) {
|
dispatch_async(dispatch_get_main_queue(), ^(void) {
|
||||||
callback(image);
|
callback(image);
|
||||||
});
|
});
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user