Skip to content

Commit

Permalink
fix(ios): implement Ti.Blob.arrayBuffer natively
Browse files Browse the repository at this point in the history
  • Loading branch information
sgtcoolguy committed Jan 13, 2021
1 parent bbb48f8 commit 46842aa
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
3 changes: 2 additions & 1 deletion iphone/TitaniumKit/TitaniumKit/Sources/API/TiBlob.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,9 @@ JSExportAs(imageWithRoundedCorner,
: (NSUInteger)cornerSize withBorder
: (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!
- (NSString *)toString;
- (JSValue *)toArrayBuffer;
- (JSValue *)arrayBuffer;

@end

Expand Down
27 changes: 27 additions & 0 deletions iphone/TitaniumKit/TitaniumKit/Sources/API/TiBlob.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#import "UIImage+Resize.h"
#import "UIImage+RoundedCorner.h"
//NOTE:FilesystemFile is conditionally compiled based on the filesystem module.
#import "KrollPromise.h"
#import "TiFilesystemFileProxy.h"

static NSString *const MIMETYPE_PNG = @"image/png";
Expand Down Expand Up @@ -476,4 +477,30 @@ - (JSValue *)toArrayBuffer
JSObjectRef arrayBuffer = JSObjectMakeArrayBufferWithBytesNoCopy(context.JSGlobalContextRef, arrayBytes, len, jsArrayBufferFreeDeallocator, nil, exception);
return [JSValue valueWithJSValueRef:arrayBuffer inContext:context];
}

- (JSValue *)arrayBuffer
{
JSContext *context = [self currentContext];
KrollPromise *promise = [[KrollPromise alloc] initInContext:context];
TiThreadPerformOnMainThread(
^{
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
JSValueRef *exception;
JSObjectRef arrayBuffer = JSObjectMakeArrayBufferWithBytesNoCopy(context.JSGlobalContextRef, arrayBytes, len, jsArrayBufferFreeDeallocator, nil, exception);
if (exception) {
[promise reject:@[ [JSValue valueWithJSValueRef:exception inContext:context] ]];
} else {
JSValue *buffer = [JSValue valueWithJSValueRef:arrayBuffer inContext:context];
[promise resolve:@[ buffer ]];
}
},
NO);
return promise.JSValue;
}
@end

0 comments on commit 46842aa

Please sign in to comment.