Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ios): Sync camera API return to match Android changes #911

Merged
merged 1 commit into from
Oct 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 46 additions & 5 deletions src/ios/CDVCamera.m
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ Licensed to the Apache Software Foundation (ASF) under one
}
}

static NSString* MIME_PNG = @"image/png";
static NSString* MIME_JPEG = @"image/jpeg";

@implementation CDVPictureOptions

+ (instancetype) createFromTakePictureArguments:(CDVInvokedUrlCommand*)command
Expand Down Expand Up @@ -372,16 +375,50 @@ - (void)popoverControllerDidDismissPopover:(id)popoverController
self.hasPendingOperation = NO;
}

- (NSData*)processImage:(UIImage*)image info:(NSDictionary*)info options:(CDVPictureOptions*)options
- (NSString*) getMimeForEncoding:(CDVEncodingType) encoding {
switch (encoding) {
case EncodingTypePNG: return MIME_PNG;
case EncodingTypeJPEG:
default:
return MIME_JPEG;
}
}

- (NSString*) formatAsDataURI:(NSData*) data withMIME:(NSString*) mime {
NSString* base64 = toBase64(data);

if (base64 == nil) {
return nil;
}

return [NSString stringWithFormat:@"data:%@;base64,%@", mime, base64];
}

- (NSString*) processImageAsDataUri:(UIImage*) image info:(NSDictionary*) info options:(CDVPictureOptions*) options
{
NSString* mime = nil;
NSData* data = [self processImage: image info: info options: options outMime: &mime];

return [self formatAsDataURI: data withMIME: mime];
}

- (NSData*) processImage:(UIImage*) image info:(NSDictionary*) info options:(CDVPictureOptions*) options
{
return [self processImage:image info: info options: options outMime: nil];
}

- (NSData*) processImage:(UIImage*)image info:(NSDictionary*)info options:(CDVPictureOptions*)options outMime:(NSString**) outMime
{
NSData* data = nil;

switch (options.encodingType) {
case EncodingTypePNG:
data = UIImagePNGRepresentation(image);
if (outMime != nil) *outMime = MIME_PNG;
break;
case EncodingTypeJPEG:
{
if (outMime != nil) *outMime = MIME_JPEG;
if ((options.allowsEditing == NO) && (options.targetSize.width <= 0) && (options.targetSize.height <= 0) && (options.correctOrientation == NO) && (([options.quality integerValue] == 100) || (options.sourceType != UIImagePickerControllerSourceTypeCamera))){
// use image unedited as requested , don't resize
data = UIImageJPEGRepresentation(image, 1.0);
Expand Down Expand Up @@ -439,7 +476,8 @@ - (NSData*)processImage:(UIImage*)image info:(NSDictionary*)info options:(CDVPic
default:
break;
};



return data;
}

Expand Down Expand Up @@ -564,16 +602,17 @@ - (void)resultForImage:(CDVPictureOptions*)options info:(NSDictionary*)info comp
case DestinationTypeDataUrl:
{
image = [self retrieveImage:info options:options];
NSData* data = [self processImage:image info:info options:options];
NSString* data = [self processImageAsDataUri:image info:info options:options];
if (data) {
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:toBase64(data)];
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString: data];
}
}
break;
default: // DestinationTypeFileUri
{
image = [self retrieveImage:info options:options];
NSData* data = [self processImage:image info:info options:options];

if (data) {
if (pickerController.sourceType == UIImagePickerControllerSourceTypePhotoLibrary) {
NSMutableData *imageDataWithExif = [NSMutableData data];
Expand Down Expand Up @@ -823,7 +862,9 @@ - (void)imagePickerControllerReturnImageResult
switch (options.destinationType) {
case DestinationTypeDataUrl:
{
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:toBase64(self.data)];
NSString* mime = [self getMimeForEncoding: self.pickerController.pictureOptions.encodingType];
NSString* uri = [self formatAsDataURI: self.data withMIME: mime];
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString: uri];
}
break;
default: // DestinationTypeFileUri
Expand Down
Loading