@@ -726,68 +726,76 @@ - (UIImage*)retrieveImage:(NSDictionary*)info options:(CDVPictureOptions*)option
726726 return processedImage;
727727}
728728
729- - (void )resultForImage : (CDVPictureOptions*)options
729+ - (void )resultForImage : (CDVPictureOptions*)pictureOptions
730730 info : (NSDictionary *)info
731731 completion : (void (^)(CDVPluginResult* res))completion
732732{
733733 CDVPluginResult* result = nil ;
734- BOOL saveToPhotoAlbum = options.saveToPhotoAlbum ;
735734 UIImage* image = nil ;
736735
737- switch (options .destinationType ) {
736+ switch (pictureOptions .destinationType ) {
738737 case DestinationTypeDataUrl:
739738 {
740- image = [self retrieveImage: info options: options ];
741- NSString * data = [self processImageAsDataUri: image info: info options: options ];
739+ image = [self retrieveImage: info options: pictureOptions ];
740+ NSString * data = [self processImageAsDataUri: image info: info options: pictureOptions ];
742741 if (data) {
743742 result = [CDVPluginResult resultWithStatus: CDVCommandStatus_OK messageAsString: data];
744743 }
745744 }
746745 break ;
747746 default : // DestinationTypeFileUri
748747 {
749- image = [self retrieveImage: info options: options ];
750- NSData * data = [self processImage: image info: info options: options ];
748+ image = [self retrieveImage: info options: pictureOptions ];
749+ NSData * imageData = [self processImage: image info: info options: pictureOptions ];
751750
752- if (data) {
753- if (self.cdvUIImagePickerController .sourceType == UIImagePickerControllerSourceTypePhotoLibrary) {
754- NSMutableData *imageDataWithExif = [NSMutableData data ];
751+ if (imageData) {
752+ if (pictureOptions.sourceType == UIImagePickerControllerSourceTypePhotoLibrary) {
753+
754+ NSData *imageDataToWrite = imageData;
755+
756+ // Copy custom choosen meta data stored in self.metadata to the image
755757 if (self.metadata ) {
756- CGImageSourceRef sourceImage = CGImageSourceCreateWithData ((__bridge CFDataRef)self.data , NULL );
758+ NSMutableData *imageDataWithCustomMetaData = [NSMutableData data ];
759+
760+ // Prepare source image
761+ CGImageSourceRef sourceImage = CGImageSourceCreateWithData ((__bridge CFDataRef)imageDataToWrite, NULL );
757762 CFStringRef sourceType = CGImageSourceGetType (sourceImage);
758-
759- CGImageDestinationRef destinationImage = CGImageDestinationCreateWithData ((__bridge CFMutableDataRef)imageDataWithExif, sourceType, 1 , NULL );
763+
764+ // Prepare dest image
765+ CGImageDestinationRef destinationImage = CGImageDestinationCreateWithData ((__bridge CFMutableDataRef)imageDataWithCustomMetaData, sourceType, 1 , NULL );
766+
767+ // Copy source to dest with self.metadata
760768 CGImageDestinationAddImageFromSource (destinationImage, sourceImage, 0 , (__bridge CFDictionaryRef)self.metadata );
761769 CGImageDestinationFinalize (destinationImage);
762770
763771 CFRelease (sourceImage);
764772 CFRelease (destinationImage);
765- } else {
766- imageDataWithExif = [ self .data mutableCopy ] ;
773+
774+ imageDataToWrite = imageDataWithCustomMetaData ;
767775 }
768776
777+ NSString * tempFilePath = [self tempFilePathForExtension: pictureOptions.encodingType == EncodingTypePNG ? @" png" :@" jpg" ];
769778 NSError * err = nil ;
770- NSString * extension = self.cdvUIImagePickerController .pictureOptions .encodingType == EncodingTypePNG ? @" png" :@" jpg" ;
771- NSString * filePath = [self tempFilePathForExtension: extension];
772-
773- // save file
774- if (![imageDataWithExif writeToFile: filePath options: NSAtomicWrite error: &err]) {
779+
780+ // Write image to temp path
781+ if ([imageDataToWrite writeToFile: tempFilePath options: NSAtomicWrite error: &err]) {
782+ result = [CDVPluginResult resultWithStatus: CDVCommandStatus_OK
783+ messageAsString: [[NSURL fileURLWithPath: tempFilePath] absoluteString ]];
784+
785+ // Write was not successful
786+ } else {
775787 result = [CDVPluginResult resultWithStatus: CDVCommandStatus_IO_EXCEPTION
776788 messageAsString: [err localizedDescription ]];
777789 }
778- else {
779- result = [CDVPluginResult resultWithStatus: CDVCommandStatus_OK
780- messageAsString: [[NSURL fileURLWithPath: filePath] absoluteString ]];
781- }
782790
783- } else if (self. cdvUIImagePickerController . sourceType != UIImagePickerControllerSourceTypeCamera || !options .usesGeolocation ) {
791+ } else if (pictureOptions. sourceType != UIImagePickerControllerSourceTypeCamera || !pictureOptions .usesGeolocation ) {
784792 // No need to save file if usesGeolocation is true since it will be saved after the location is tracked
785- NSString * extension = options .encodingType == EncodingTypePNG? @" png" : @" jpg" ;
793+ NSString * extension = pictureOptions .encodingType == EncodingTypePNG? @" png" : @" jpg" ;
786794 NSString * filePath = [self tempFilePathForExtension: extension];
787795 NSError * err = nil ;
788796
789797 // save file
790- if (![data writeToFile: filePath options: NSAtomicWrite error: &err]) {
798+ if (![imageData writeToFile: filePath options: NSAtomicWrite error: &err]) {
791799 result = [CDVPluginResult resultWithStatus: CDVCommandStatus_IO_EXCEPTION
792800 messageAsString: [err localizedDescription ]];
793801 } else {
@@ -801,7 +809,8 @@ - (void)resultForImage:(CDVPictureOptions*)options
801809 break ;
802810 };
803811
804- if (saveToPhotoAlbum && image) {
812+ // Save the image to the photo album after capture
813+ if (pictureOptions.saveToPhotoAlbum && image) {
805814 UIImageWriteToSavedPhotosAlbum (image, nil , nil , nil );
806815 }
807816
0 commit comments