From 64475f11cd5bb9b97f54dfe2a62ef14ed6903919 Mon Sep 17 00:00:00 2001 From: Norman Breau Date: Sat, 26 Oct 2024 10:15:53 -0300 Subject: [PATCH] fix(ios): Sync camera API return to match Android changes --- src/ios/CDVCamera.m | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/src/ios/CDVCamera.m b/src/ios/CDVCamera.m index 6793da45b..cbd76481a 100644 --- a/src/ios/CDVCamera.m +++ b/src/ios/CDVCamera.m @@ -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 @@ -372,16 +375,37 @@ - (void)popoverControllerDidDismissPopover:(id)popoverController self.hasPendingOperation = NO; } -- (NSData*)processImage:(UIImage*)image info:(NSDictionary*)info options:(CDVPictureOptions*)options +- (NSString*) processImageAsDataUri:(UIImage*) image info:(NSDictionary*) info options:(CDVPictureOptions*) options +{ + NSString* mime = nil; + NSData* data = [self processImage: image info: info options: options outMime: &mime]; + + NSString* base64 = toBase64(data); + + if (base64 == nil) { + return nil; + } + + return [NSString stringWithFormat:@"data:%@;base64,%@", mime, base64]; +} + +- (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); @@ -439,7 +463,8 @@ - (NSData*)processImage:(UIImage*)image info:(NSDictionary*)info options:(CDVPic default: break; }; - + + return data; } @@ -564,9 +589,9 @@ - (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; @@ -574,6 +599,7 @@ - (void)resultForImage:(CDVPictureOptions*)options info:(NSDictionary*)info comp { 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];