iOS: implement Image Pixel
This commit is contained in:
@@ -20,8 +20,14 @@
|
||||
// Created by pengfei.zhou on 2019/7/25.
|
||||
//
|
||||
|
||||
#import <DoricCore/DoricExtensions.h>
|
||||
#import "DoricJSCoreExecutor.h"
|
||||
|
||||
void ReleaseArrayBufferData(void *bytes, void *deallocatorContext) {
|
||||
id data = (__bridge_transfer id) deallocatorContext;
|
||||
data = nil;
|
||||
}
|
||||
|
||||
@interface DoricJSCoreExecutor ()
|
||||
|
||||
@property(nonatomic, strong) JSContext *jsContext;
|
||||
@@ -57,7 +63,19 @@ - (void)injectGlobalJSObject:(NSString *)name obj:(id)obj {
|
||||
|
||||
- (JSValue *)invokeObject:(NSString *)objName method:(NSString *)funcName args:(NSArray *)args {
|
||||
JSValue *obj = [self.jsContext objectForKeyedSubscript:objName];
|
||||
JSValue *ret = [obj invokeMethod:funcName withArguments:args];
|
||||
JSValue *ret = [obj invokeMethod:funcName withArguments:[args map:^id(id obj) {
|
||||
if ([obj isKindOfClass:NSData.class]) {
|
||||
NSData *data = (NSData *) obj;
|
||||
JSObjectRef jsObject = JSObjectMakeArrayBufferWithBytesNoCopy(self.jsContext.JSGlobalContextRef,
|
||||
(void *) data.bytes,
|
||||
data.length,
|
||||
ReleaseArrayBufferData,
|
||||
(__bridge_retained void *) data,
|
||||
NULL);
|
||||
return [JSValue valueWithJSValueRef:jsObject inContext:self.jsContext];
|
||||
}
|
||||
return obj;
|
||||
}]];
|
||||
[self checkJSException];
|
||||
return ret;
|
||||
}
|
||||
|
12
doric-iOS/Pod/Classes/Engine/JSValue+Doric.h
Normal file
12
doric-iOS/Pod/Classes/Engine/JSValue+Doric.h
Normal file
@@ -0,0 +1,12 @@
|
||||
//
|
||||
// Created by pengfei.zhou on 2021/11/19.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import <JavaScriptCore/JavaScriptCore.h>
|
||||
|
||||
@interface JSValue (Doric)
|
||||
- (BOOL)isArrayBuffer;
|
||||
|
||||
- (NSData *)toArrayBuffer;
|
||||
@end
|
31
doric-iOS/Pod/Classes/Engine/JSValue+Doric.m
Normal file
31
doric-iOS/Pod/Classes/Engine/JSValue+Doric.m
Normal file
@@ -0,0 +1,31 @@
|
||||
//
|
||||
// Created by pengfei.zhou on 2021/11/19.
|
||||
//
|
||||
|
||||
#import "JSValue+Doric.h"
|
||||
|
||||
@implementation JSValue (Doric)
|
||||
- (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];
|
||||
}
|
||||
|
||||
@end
|
@@ -25,6 +25,8 @@
|
||||
#import "DoricUtil.h"
|
||||
#import "DoricSuperNode.h"
|
||||
#import "DoricThirdParty.h"
|
||||
#import <JavaScriptCore/JavaScriptCore.h>
|
||||
#import <JSValue+Doric.h>
|
||||
|
||||
#if DORIC_USE_YYWEBIMAGE
|
||||
|
||||
@@ -576,6 +578,33 @@ - (void)blendView:(UIImageView *)view forPropName:(NSString *)name propValue:(id
|
||||
options:NSKeyValueObservingOptionNew
|
||||
context:nil];
|
||||
#endif
|
||||
} else if ([@"imagePixels" isEqualToString:name]) {
|
||||
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;
|
||||
}];
|
||||
}];
|
||||
} else {
|
||||
[super blendView:view forPropName:name propValue:prop];
|
||||
}
|
||||
@@ -711,5 +740,35 @@ - (void)dealloc {
|
||||
[(DoricImageView *) self.view removeObserver:self forKeyPath:@"currentFrameIndex" context:nil];
|
||||
#endif
|
||||
}
|
||||
- (NSDictionary *)getImageInfo {
|
||||
CGImageRef imageRef = [self.view.image CGImage];
|
||||
return @{
|
||||
@"width": @(CGImageGetWidth(imageRef)),
|
||||
@"height": @(CGImageGetHeight(imageRef)),
|
||||
};
|
||||
}
|
||||
|
||||
- (NSData *)getImagePixels {
|
||||
CGImageRef imageRef = [self.view.image CGImage];
|
||||
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
|
||||
size_t width = CGImageGetWidth(imageRef);
|
||||
size_t height = CGImageGetHeight(imageRef);
|
||||
size_t bytesPerPixel = 4;
|
||||
size_t bytesPerRow = bytesPerPixel * width;
|
||||
|
||||
unsigned char *imageData = malloc(width * height * bytesPerPixel);
|
||||
CGContextRef contextRef = CGBitmapContextCreate(
|
||||
imageData,
|
||||
width,
|
||||
height,
|
||||
8,
|
||||
bytesPerRow,
|
||||
colorSpace,
|
||||
kCGImageAlphaPremultipliedLast);
|
||||
CGContextDrawImage(contextRef, CGRectMake(0, 0, width, height), imageRef);
|
||||
CGColorSpaceRelease(colorSpace);
|
||||
CGContextRelease(contextRef);
|
||||
|
||||
return [[NSData alloc] initWithBytesNoCopy:imageData length:width * height * bytesPerPixel];
|
||||
}
|
||||
@end
|
||||
|
Reference in New Issue
Block a user