iOS: implement Image Pixel

This commit is contained in:
pengfei.zhou
2021-11-22 11:54:47 +08:00
committed by osborn
parent 190eb4afd7
commit b29f2d6a4e
20 changed files with 365 additions and 9 deletions

View File

@@ -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;
}

View 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

View 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