You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by ma...@apache.org on 2026/02/03 11:36:46 UTC

(cordova-plugin-camera) 01/01: refactor(ios): small refactoring on `resultForImage:`

This is an automated email from the ASF dual-hosted git repository.

manuelbeck pushed a commit to branch pr-resultforimage-small-refactoring
in repository https://gitbox.apache.org/repos/asf/cordova-plugin-camera.git

commit 64b28461c64dfd1c9450d2f8bb477832bb4f3ac4
Author: Manuel Beck <ma...@outlook.de>
AuthorDate: Tue Feb 3 12:36:34 2026 +0100

    refactor(ios): small refactoring on `resultForImage:`
    
    - Document meta data processing
    - Remove any reference of `self.cdvUIImagePickerController` since it is not needed here and also not set, when `PHPickerViewController` is used
    - Use `pictureOptions.sourceType` instead of `self.cdvUIImagePickerController.sourceType``
    - Use `pictureOptions` parameter instead of `self.cdvUIImagePickerController.pictureOptions`
---
 src/ios/CDVCamera.m | 61 ++++++++++++++++++++++++++++++-----------------------
 1 file changed, 35 insertions(+), 26 deletions(-)

diff --git a/src/ios/CDVCamera.m b/src/ios/CDVCamera.m
index 0cdf306..b46303e 100644
--- a/src/ios/CDVCamera.m
+++ b/src/ios/CDVCamera.m
@@ -740,19 +740,18 @@ static NSString* MIME_JPEG    = @"image/jpeg";
     return (scaledImage == nil ? image : scaledImage);
 }
 
-- (void)resultForImage:(CDVPictureOptions*)options
+- (void)resultForImage:(CDVPictureOptions*)pictureOptions
                   info:(NSDictionary*)info
             completion:(void (^)(CDVPluginResult* res))completion
 {
     CDVPluginResult* result = nil;
-    BOOL saveToPhotoAlbum = options.saveToPhotoAlbum;
     UIImage* image = nil;
 
-    switch (options.destinationType) {
+    switch (pictureOptions.destinationType) {
         case DestinationTypeDataUrl:
         {
-            image = [self retrieveImage:info options:options];
-            NSString* data = [self processImageAsDataUri:image info:info options:options];
+            image = [self retrieveImage:info options:pictureOptions];
+            NSString* data = [self processImageAsDataUri:image info:info options:pictureOptions];
             if (data)  {
                 result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString: data];
             }
@@ -760,48 +759,57 @@ static NSString* MIME_JPEG    = @"image/jpeg";
             break;
         default: // DestinationTypeFileUri
         {
-            image = [self retrieveImage:info options:options];
-            NSData* data = [self processImage:image info:info options:options];
+            image = [self retrieveImage:info options:pictureOptions];
+            NSData* imageData = [self processImage:image info:info options:pictureOptions];
             
-            if (data) {
-                if (self.cdvUIImagePickerController.sourceType == UIImagePickerControllerSourceTypePhotoLibrary) {
-                    NSMutableData *imageDataWithExif = [NSMutableData data];
+            if (imageData) {
+                if (pictureOptions.sourceType == UIImagePickerControllerSourceTypePhotoLibrary) {
+                    
+                    // Copy custom choosen meta data stored in self.metadata to the image
+                    // This will make the image smaller
+                    NSMutableData *imageDataWithExif = nil;
+                    
                     if (self.metadata) {
+                        imageDataWithExif = [NSMutableData data];
+                        
+                        // Prepare source image
                         CGImageSourceRef sourceImage = CGImageSourceCreateWithData((__bridge CFDataRef)self.data, NULL);
                         CFStringRef sourceType = CGImageSourceGetType(sourceImage);
-
+                        
+                        // Prepare dest image
                         CGImageDestinationRef destinationImage = CGImageDestinationCreateWithData((__bridge CFMutableDataRef)imageDataWithExif, sourceType, 1, NULL);
+                        
+                        // Copy source to dest with metadata
                         CGImageDestinationAddImageFromSource(destinationImage, sourceImage, 0, (__bridge CFDictionaryRef)self.metadata);
                         CGImageDestinationFinalize(destinationImage);
 
                         CFRelease(sourceImage);
                         CFRelease(destinationImage);
-                    } else {
-                        imageDataWithExif = [self.data mutableCopy];
                     }
 
+                    NSData *imageDataToWrite = imageDataWithExif != nil ? imageDataWithExif : imageData;
+                    NSString* tempFilePath = [self tempFilePathForExtension:pictureOptions.encodingType == EncodingTypePNG ? @"png":@"jpg"];
                     NSError* err = nil;
-                    NSString* extension = self.cdvUIImagePickerController.pictureOptions.encodingType == EncodingTypePNG ? @"png":@"jpg";
-                    NSString* filePath = [self tempFilePathForExtension:extension];
-
-                    // save file
-                    if (![imageDataWithExif writeToFile:filePath options:NSAtomicWrite error:&err]) {
+                    
+                    // Write image to temp path
+                    if ([imageDataToWrite writeToFile:tempFilePath options:NSAtomicWrite error:&err]) {
+                        result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK
+                                                   messageAsString:[[NSURL fileURLWithPath:tempFilePath] absoluteString]];
+                        
+                        // Write was not successful
+                    } else {
                         result = [CDVPluginResult resultWithStatus:CDVCommandStatus_IO_EXCEPTION
                                                    messageAsString:[err localizedDescription]];
                     }
-                    else {
-                        result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK
-                                                   messageAsString:[[NSURL fileURLWithPath:filePath] absoluteString]];
-                    }
                     
-                } else if (self.cdvUIImagePickerController.sourceType != UIImagePickerControllerSourceTypeCamera || !options.usesGeolocation) {
+                } else if (pictureOptions.sourceType != UIImagePickerControllerSourceTypeCamera || !pictureOptions.usesGeolocation) {
                     // No need to save file if usesGeolocation is true since it will be saved after the location is tracked
-                    NSString* extension = options.encodingType == EncodingTypePNG? @"png" : @"jpg";
+                    NSString* extension = pictureOptions.encodingType == EncodingTypePNG? @"png" : @"jpg";
                     NSString* filePath = [self tempFilePathForExtension:extension];
                     NSError* err = nil;
 
                     // save file
-                    if (![data writeToFile:filePath options:NSAtomicWrite error:&err]) {
+                    if (![imageData writeToFile:filePath options:NSAtomicWrite error:&err]) {
                         result = [CDVPluginResult resultWithStatus:CDVCommandStatus_IO_EXCEPTION
                                                    messageAsString:[err localizedDescription]];
                     } else {
@@ -815,7 +823,8 @@ static NSString* MIME_JPEG    = @"image/jpeg";
             break;
     };
 
-    if (saveToPhotoAlbum && image) {
+    // Save the image to the photo album after capture
+    if (pictureOptions.saveToPhotoAlbum && image) {
         UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
     }
 


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cordova.apache.org
For additional commands, e-mail: commits-help@cordova.apache.org