Skip to content

Commit

Permalink
feat(ios): add Ti.Blob.toArrayBuffer()
Browse files Browse the repository at this point in the history
creates a JS ArrayBuffer with a copy of the underlying bytes from the blob
  • Loading branch information
sgtcoolguy committed Aug 13, 2020
1 parent 38851d7 commit e42bbcb
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
1 change: 1 addition & 0 deletions iphone/TitaniumKit/TitaniumKit/Sources/API/TiBlob.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ JSExportAs(imageWithRoundedCorner,
: (NSNumber *)optionalBorderSize);
- (TiBlob *)imageWithTransparentBorder:(NSUInteger)size;
- (NSString *)toString; // FIXME This doesn't seem to override the JS impl. I think we need to find a way to modify the property post-init to override it!
- (JSValue *)toArrayBuffer;

@end

Expand Down
19 changes: 19 additions & 0 deletions iphone/TitaniumKit/TitaniumKit/Sources/API/TiBlob.m
Original file line number Diff line number Diff line change
Expand Up @@ -446,4 +446,23 @@ - (NSString *)toString
return [super toString];
}

static void jsArrayBufferFreeDeallocator(void *data, void *ctx)
{
free(data);
}

- (JSValue *)toArrayBuffer
{
NSData *theData = [self data];
// Copy the raw bytes of the NSData we're wrapping
NSUInteger len = [theData length];
void *arrayBytes = malloc(len);
[theData getBytes:arrayBytes length:len];

// Now make an ArrayBuffer with the copied bytes
JSContext *context = JSContext.currentContext;
JSValueRef *exception;
JSObjectRef arrayBuffer = JSObjectMakeArrayBufferWithBytesNoCopy(context.JSGlobalContextRef, arrayBytes, len, jsArrayBufferFreeDeallocator, nil, exception);
return [JSValue valueWithJSValueRef:arrayBuffer inContext:context];
}
@end

0 comments on commit e42bbcb

Please sign in to comment.