1

我使用 AV Foundation 捕获静止图像并保存到相机胶卷,如下所示:

- (void) captureStillImage
{
    AVCaptureConnection *stillImageConnection =
    [self.stillImageOutput.connections objectAtIndex:0];
    if ([stillImageConnection isVideoOrientationSupported])
        [stillImageConnection setVideoOrientation:AVCaptureVideoOrientationPortrait];

    [self.stillImageOutput
     captureStillImageAsynchronouslyFromConnection:stillImageConnection
     completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error)
     {
         if (imageDataSampleBuffer != NULL)
         {
             NSData *imageData = [AVCaptureStillImageOutput
                                  jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
             ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
             UIImage *image = [[UIImage alloc] initWithData:imageData];
             [library writeImageToSavedPhotosAlbum:[image CGImage]
                                       orientation:(ALAssetOrientation)[image imageOrientation]
                                   completionBlock:^(NSURL *assetURL, NSError *error){
              }];
         }
         else
         {
             NSLog(@"Error capturing still image: %@", error);
         }
     }
     ];
}

在照片应用程序中再次检查时,我的应用程序中的这些图像没有关于城市名称的信息。

如何捕获静止图像并使用可显示在照片应用程序中的位置名称保存?

感谢帮助!

4

1 回答 1

1

来自文档:captureStillImageAsynchronouslyFromConnection

处理程序

捕获图像后要调用的块。块参数如下: imageDataSampleBuffer 捕获的数据。缓冲区附件可能包含适合图像数据格式的元数据。例如,包含 JPEG 数据的缓冲区可以携带 kCGImagePropertyExifDictionary 作为附件。有关键和值类型的列表,请参阅 ImageIO/CGImageProperties.h。

所以使用它你可以获得元数据。

         CFDictionaryRef metaDict = CMCopyDictionaryOfAttachments(NULL, imageDataSampleBuffer, kCMAttachmentMode_ShouldPropagate);
         CFMutableDictionaryRef mutable = CFDictionaryCreateMutableCopy(NULL, 0, metaDict);


         NSDictionary *metaDict = [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];
         NSLog(@"%@",metaDict);
         CFDictionarySetValue(mutable, kCGImagePropertyGPSDictionary, (__bridge const void *)(metaDict));

并在保存到资产库时使用此方法添加元数据

[library writeImageToSavedPhotosAlbum:[image CGImage] metadata:mutable completionBlock: nil];
于 2013-10-22T09:08:46.070 回答