将元数据写入图像时出现问题

3 cocoa-touch metadata avfoundation

我正在使用AvFoundation拍摄静态图像并将gps信息添加到元数据并使用资源库保存到相册,但gps信息根本不保存.

这是我的代码......

[self.stillImageTaker captureStillImageAsynchronouslyFromConnection:videoConnection
                    completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) 
    {

if (imageDataSampleBuffer != NULL) 

    {

            CFDictionaryRef exifAttachments = CMGetAttachment(imageDataSampleBuffer,kCGImagePropertyExifDictionary, NULL);
            CFDictionaryRef metadataDict = CMCopyDictionaryOfAttachments(NULL, imageDataSampleBuffer, kCMAttachmentMode_ShouldPropagate);


        NSDictionary *gpsDict = [NSDictionary dictionaryWithObjectsAndKeys:@"1",kCGImagePropertyGPSVersion,
                                 @"78.4852",kCGImagePropertyGPSLatitude,@"32.1456",kCGImagePropertyGPSLongitude, nil];

        CMSetAttachment(imageDataSampleBuffer,kCGImagePropertyGPSDictionary,gpsDict,kCMAttachmentMode_ShouldPropagate);

        CFDictionaryRef newMetadata = CMCopyDictionaryOfAttachments(NULL, imageDataSampleBuffer, kCMAttachmentMode_ShouldPropagate);
        CFDictionaryRef gpsAttachments = CMGetAttachment(imageDataSampleBuffer,kCGImagePropertyGPSDictionary, NULL);

        if (exifAttachments) 
        { // Attachments may be read or additional ones written

        }

        NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
        UIImage *image = [[UIImage alloc] initWithData:imageData];  

        ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
        /


        NSDictionary *newDict = (NSDictionary *)newMetadata;

        [library writeImageToSavedPhotosAlbum:[image CGImage] 
                                     metadata:newDict completionBlock:^(NSURL *assetURL, NSError *error)
         {
             if (error) 
             {

             }                                                                                               
         }];

        [library release];
        [image release];
        CFRelease(metadataDict);
        CFRelease(newMetadata);

    } 
    else if (error) 
    {

    }

}];
Run Code Online (Sandbox Code Playgroud)

Mas*_*son 9

我有完全相同的问题.我认为关于这个主题的文档不是很好,所以我最终通过查看Camera应用程序拍摄的照片的元数据并尝试复制它来解决它.

以下是Camera应用程序保存的属性的向下运行:

  • kCGImagePropertyGPSLatitude (NSNumber) (十进制格式的纬度)
  • kCGImagePropertyGPSLongitude (NSNumber) (十进制格式的经度)
  • kCGImagePropertyGPSLatitudeRef (NSString) (N或S)
  • kCGImagePropertyGPSLongitudeRef (NSString) (E或W)
  • kCGImagePropertyGPSTimeStamp (NSString) (格式为04:30:51.71(UTC时间戳))

如果你坚持这些你应该没事.这是一个示例:

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];
Run Code Online (Sandbox Code Playgroud)

这都在AVCaptureConnection对象的captureStillImageAsynchronouslyFromConnection方法的completionHandler中,而self.currentLocation只是一个CLLocation.我为这个例子硬编了时间戳和Lat/Lng Refs以保持简单.

希望这可以帮助!