Problem in writing metadata to image

后端 未结 3 1650
灰色年华
灰色年华 2021-01-01 06:19

I am using AvFoundation to take still image and adding gps info to metadata and saving to a photo album using Asset library but gps info is not saving at all.

here

3条回答
  •  天命终不由人
    2021-01-01 06:56

    I had exactly the same problem. I think the documentation on this topic isn't great, so I solved it in the end by looking at the metadata of a photo taken by the Camera app and trying to replicate it.

    Here's a run down of the properties the Camera app saves:

    • kCGImagePropertyGPSLatitude (NSNumber) (Latitude in decimal format)
    • kCGImagePropertyGPSLongitude (NSNumber) (Longitude in decimal format)
    • kCGImagePropertyGPSLatitudeRef (NSString) (Either N or S)
    • kCGImagePropertyGPSLongitudeRef (NSString) (Either E or W)
    • kCGImagePropertyGPSTimeStamp (NSString) (Of the format 04:30:51.71 (UTC timestamp))

    If you stick to these you should be fine. Here's a sample:

    CFDictionaryRef metaDict = CMCopyDictionaryOfAttachments(NULL, imageDataSampleBuffer, kCMAttachmentMode_ShouldPropagate);
    CFMutableDictionaryRef mutable = CFDictionaryCreateMutableCopy(NULL, 0, metaDict);
    
    NSDictionary *gpsDict = [NSDictionary 
      dictionaryWithObjectsAndKeys:
      [NSNumber numberWithFloat:self.currentLocation.coordinate.latitude], kCGImagePropertyGPSLatitude,
      @"N", kCGImagePropertyGPSLatitudeRef,
      [NSNumber numberWithFloat:self.currentLocation.coordinate.longitude], kCGImagePropertyGPSLongitude,
      @"E", kCGImagePropertyGPSLongitudeRef,
      @"04:30:51.71", kCGImagePropertyGPSTimeStamp,
      nil];
    
    CFDictionarySetValue(mutable, kCGImagePropertyGPSDictionary, gpsDict);
    
    //  Get the image
    NSData *imageData = [AVCaptureStillImageOutput  jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
    UIImage *image = [[UIImage alloc] initWithData:imageData];
    
    //  Get the assets library
    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
    [library writeImageToSavedPhotosAlbum:[image CGImage] metadata:mutable completionBlock:captureComplete];
    

    This is all in the completionHandler of the captureStillImageAsynchronouslyFromConnection method of your AVCaptureConnection object, and self.currentLocation is just a CLLocation. I hardcoded the timestamp and Lat/Lng Refs for the example to keep things simple.

    Hope this helps!

提交回复
热议问题