feat: Image add setImagePixels API,iOS receive ArrayBuffer directly
This commit is contained in:
@@ -6,9 +6,5 @@
|
||||
#import <JavaScriptCore/JavaScriptCore.h>
|
||||
|
||||
@interface JSValue (Doric)
|
||||
- (BOOL)isArrayBuffer;
|
||||
|
||||
- (NSData *)toArrayBuffer;
|
||||
|
||||
- (id)toObjectWithArrayBuffer;
|
||||
@end
|
@@ -159,29 +159,6 @@ id valueToObject(JSContext *context, JSValueRef value) {
|
||||
return containerValueToObject([context JSGlobalContextRef], result);
|
||||
}
|
||||
|
||||
- (BOOL)isArrayBuffer {
|
||||
JSContextRef ctx = self.context.JSGlobalContextRef;
|
||||
JSValueRef jsValueRef = self.JSValueRef;
|
||||
if (self.isObject) {
|
||||
JSTypedArrayType type = JSValueGetTypedArrayType(ctx, jsValueRef, NULL);
|
||||
return type == kJSTypedArrayTypeArrayBuffer;
|
||||
}
|
||||
return NO;
|
||||
}
|
||||
|
||||
- (NSData *)toArrayBuffer {
|
||||
if (!self.isArrayBuffer) {
|
||||
return nil;
|
||||
}
|
||||
JSContextRef ctx = self.context.JSGlobalContextRef;
|
||||
JSValueRef jsValueRef = self.JSValueRef;
|
||||
JSObjectRef ref = JSValueToObject(ctx, jsValueRef, NULL);
|
||||
size_t size = JSObjectGetArrayBufferByteLength(ctx, ref, NULL);
|
||||
void *ptr = JSObjectGetArrayBufferBytesPtr(ctx, ref, NULL);
|
||||
|
||||
return [[NSData alloc] initWithBytesNoCopy:ptr length:size freeWhenDone:NO];
|
||||
}
|
||||
|
||||
- (id)toObjectWithArrayBuffer {
|
||||
return valueToObject(self.context, self.JSValueRef);
|
||||
}
|
||||
|
@@ -54,6 +54,7 @@ - (void)displayLayer:(CALayer *)layer {
|
||||
#elif DORIC_USE_SDWEBIMAGE
|
||||
|
||||
#import <SDWebImage/SDWebImage.h>
|
||||
#import <DoricPromise.h>
|
||||
|
||||
@interface DoricImageView : SDAnimatedImageView
|
||||
@end
|
||||
@@ -582,29 +583,21 @@ - (void)blendView:(UIImageView *)view forPropName:(NSString *)name propValue:(id
|
||||
NSDictionary *imagePixels = prop;
|
||||
NSUInteger width = [imagePixels[@"width"] unsignedIntValue];
|
||||
NSUInteger height = [imagePixels[@"height"] unsignedIntValue];
|
||||
NSString *pixelsCallbackId = imagePixels[@"pixels"];
|
||||
[[self callJSResponse:pixelsCallbackId, nil] setResultCallback:^(JSValue *pixelsValue) {
|
||||
if (![pixelsValue isArrayBuffer]) {
|
||||
return;
|
||||
}
|
||||
NSData *data = [pixelsValue toArrayBuffer];
|
||||
[self.doricContext.driver ensureSyncInMainQueue:^{
|
||||
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
|
||||
CGContextRef context = CGBitmapContextCreate((void *) data.bytes,
|
||||
width,
|
||||
height,
|
||||
8,
|
||||
width * 4,
|
||||
colorSpace,
|
||||
kCGImageAlphaPremultipliedLast);
|
||||
CGImageRef imageRef = CGBitmapContextCreateImage(context);
|
||||
UIImage *image = [[UIImage alloc] initWithCGImage:imageRef scale:UIScreen.mainScreen.scale orientation:UIImageOrientationUp];
|
||||
CGImageRelease(imageRef);
|
||||
CGContextRelease(context);
|
||||
CGColorSpaceRelease(colorSpace);
|
||||
self.view.image = image;
|
||||
}];
|
||||
}];
|
||||
NSData *pixels = imagePixels[@"pixels"];
|
||||
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
|
||||
CGContextRef context = CGBitmapContextCreate((void *) pixels.bytes,
|
||||
width,
|
||||
height,
|
||||
8,
|
||||
width * 4,
|
||||
colorSpace,
|
||||
kCGImageAlphaPremultipliedLast);
|
||||
CGImageRef imageRef = CGBitmapContextCreateImage(context);
|
||||
UIImage *image = [[UIImage alloc] initWithCGImage:imageRef scale:UIScreen.mainScreen.scale orientation:UIImageOrientationUp];
|
||||
CGImageRelease(imageRef);
|
||||
CGContextRelease(context);
|
||||
CGColorSpaceRelease(colorSpace);
|
||||
view.image = image;
|
||||
} else {
|
||||
[super blendView:view forPropName:name propValue:prop];
|
||||
}
|
||||
@@ -741,6 +734,9 @@ - (NSDictionary *)getImageInfo {
|
||||
}
|
||||
|
||||
- (NSData *)getImagePixels {
|
||||
if (!self.view.image) {
|
||||
return nil;
|
||||
}
|
||||
CGImageRef imageRef = [self.view.image CGImage];
|
||||
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
|
||||
size_t width = CGImageGetWidth(imageRef);
|
||||
@@ -760,10 +756,30 @@ - (NSData *)getImagePixels {
|
||||
CGContextDrawImage(contextRef, CGRectMake(0, 0, width, height), imageRef);
|
||||
CGColorSpaceRelease(colorSpace);
|
||||
CGContextRelease(contextRef);
|
||||
|
||||
return [[NSData alloc] initWithBytesNoCopy:imageData length:width * height * bytesPerPixel freeWhenDone:YES];
|
||||
}
|
||||
|
||||
- (void)setImagePixels:(NSDictionary *)imagePixels withPromise:(DoricPromise *)promise {
|
||||
NSUInteger width = [imagePixels[@"width"] unsignedIntValue];
|
||||
NSUInteger height = [imagePixels[@"height"] unsignedIntValue];
|
||||
NSData *pixels = imagePixels[@"pixels"];
|
||||
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
|
||||
CGContextRef context = CGBitmapContextCreate((void *) pixels.bytes,
|
||||
width,
|
||||
height,
|
||||
8,
|
||||
width * 4,
|
||||
colorSpace,
|
||||
kCGImageAlphaPremultipliedLast);
|
||||
CGImageRef imageRef = CGBitmapContextCreateImage(context);
|
||||
UIImage *image = [[UIImage alloc] initWithCGImage:imageRef scale:UIScreen.mainScreen.scale orientation:UIImageOrientationUp];
|
||||
CGImageRelease(imageRef);
|
||||
CGContextRelease(context);
|
||||
CGColorSpaceRelease(colorSpace);
|
||||
self.view.image = image;
|
||||
[promise resolve:nil];
|
||||
}
|
||||
|
||||
- (void)dealloc {
|
||||
if (self.animationEndCallbackId) {
|
||||
#if DORIC_USE_YYWEBIMAGE
|
||||
|
Reference in New Issue
Block a user