2020#import " AttachmentSelectionDelegate.h"
2121#import < Server+helper.h>
2222#import < Event+helper.h>
23+ #import < ImageIO/ImageIO.h>
2324#import " ObservationEditTextFieldTableViewCell.h"
2425
2526@interface ObservationEditViewController () <UIImagePickerControllerDelegate, UINavigationControllerDelegate, AudioRecordingDelegate, AttachmentSelectionDelegate>
@@ -193,6 +194,7 @@ - (void)imagePickerController:(UIImagePickerController *)picker didFinishPicking
193194 }
194195 } else {
195196 UIImage *chosenImage = info[UIImagePickerControllerOriginalImage];
197+ NSMutableDictionary *imageMetadata = [[info objectForKey: UIImagePickerControllerMediaMetadata] mutableCopy ];
196198
197199 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc ] init ];
198200 [dateFormatter setDateFormat: @" yyyymmdd_HHmmss" ];
@@ -213,12 +215,34 @@ - (void)imagePickerController:(UIImagePickerController *)picker didFinishPicking
213215 NSLog (@" Error creating directory path: %@ " , [error localizedDescription ]);
214216 }
215217
216- NSData *imageData = UIImagePNGRepresentation (chosenImage);
217- BOOL success = [imageData writeToFile: fileToWriteTo atomically: NO ];
218+ NSData *imageData = UIImageJPEGRepresentation (chosenImage, 1 .0f );
219+ CGImageSourceRef source = CGImageSourceCreateWithData ((__bridge CFDataRef) imageData, NULL );
220+ CFStringRef UTI = CGImageSourceGetType (source);
221+ NSMutableData *destinationData = [NSMutableData data ];
222+ CGImageDestinationRef destination = CGImageDestinationCreateWithData ((__bridge CFMutableDataRef) destinationData, UTI, 1 , NULL );
223+
224+ if (!destinationData) {
225+ NSLog (@" Error: Could not create image destination" );
226+ }
227+
228+ // add the image contained in the image source to the destination, overidding the old metadata with our modified metadata
229+ CGImageDestinationAddImageFromSource (destination, source, 0 , (__bridge CFDictionaryRef) imageMetadata);
230+ BOOL success = NO ;
231+ success = CGImageDestinationFinalize (destination);
232+
233+ success = [destinationData writeToFile: fileToWriteTo atomically: NO ];
234+
235+ if (!success) {
236+ NSLog (@" Error: Could not create data from image destination" );
237+ }
238+
239+ CFRelease (destination);
240+ CFRelease (source);
241+
218242 NSLog (@" successfully wrote file %d " , success);
219243
220244 NSMutableDictionary *attachmentJson = [NSMutableDictionary dictionary ];
221- [attachmentJson setValue: @" image/png " forKey: @" contentType" ];
245+ [attachmentJson setValue: @" image/jpeg " forKey: @" contentType" ];
222246 [attachmentJson setValue: fileToWriteTo forKey: @" localPath" ];
223247 [attachmentJson setValue: [NSString stringWithFormat: @" MAGE_%@ .png" , [dateFormatter stringFromDate: [NSDate date ]]] forKey: @" name" ];
224248 [attachmentJson setValue: [NSNumber numberWithBool: YES ] forKey: @" dirty" ];
0 commit comments