1

I need to use the iOS device camera to take a photo and then upload it to the server. The problem is that, by default, the UIImage taken form UIImagePickerController info does not include GPS metadata.

Following this post I have been able to save the image in the Camerra Roll with the correct GPS info. The code used is the following one:

- (void)saveImage:(UIImage *)imageToSave withInfo:(NSDictionary *)info {
    // Get the image metadata (EXIF & TIFF)
    NSMutableDictionary * imgMtd = [[info objectForKey:UIImagePickerControllerMediaMetadata] mutableCopy];

    // create CLLocation for image
    locationManager = [[CLLocationManager alloc] init];
    [locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
    CLLocation * loc = [locationManager location];

    if (loc) {
        [imgMtd setObject:[self gpsDictionaryForLocation:loc] forKey:(NSString*)kCGImagePropertyGPSDictionary];
    }

    // Get the assets library
    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];

    // create a completion block for when we process the image
    ALAssetsLibraryWriteImageCompletionBlock imageWriteCompletionBlock =
    ^(NSURL *newURL, NSError *error) {
        if (error) {
            NSLog( @"Error writing image with metadata to Photo Library: %@", error );
        } else {
            NSLog( @"Wrote image %@ with metadata %@ to Photo Library",newURL,imgMtd);
        }
    };

    // Save the new image to the Camera Roll, using the completion block defined just above
    [library writeImageToSavedPhotosAlbum:[imageToSave CGImage] metadata:imgMtd completionBlock:imageWriteCompletionBlock];

}

However, I am using this photo to upload it to a server. Therefore, the desired behavior is to save it at a temporal path (so the image won't be available at the Camera Roll), upload it to the server and then delete it. With the code provided, I am making the image public, and don't know its path to upload it.

Community
  • 1
  • 1
Matias
  • 541
  • 5
  • 22
  • `newURL` seems to be what you want? – zc246 Apr 04 '16 at 14:15
  • @zcui93 That would solve the issue of where this image is saved. Still, before uploading it to the server, I perform some optimizations to the image that imply changing its name. I cannot do that on the Camera Roll images, and that's why I need to save it in a temporal (and private) path. – Matias Apr 04 '16 at 14:56
  • What about uploading the meta data separately then? – zc246 Apr 04 '16 at 15:32
  • @zcui93 I prefer avoiding that solution. I have tried making a copy of the image saved in the camera roll, which will be updated and modified. Still, when getting the asset for newUrl I can get the metadata dictionary (OK) but when I try to get the NSData, it does not include the metadata. – Matias Apr 04 '16 at 15:37

0 Answers0