如何使用AV Foundation相机拍摄静止图像并使用其城市名称保存?

时间:2013-10-18 10:31:33

标签: ios avfoundation core-location

我拍摄静止图像并使用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);
         }
     }
     ];
}

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

如何拍摄静止图像并使用可在照片应用中显示的位置名称进行保存?

感谢您的帮助!

1 个答案:

答案 0 :(得分: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];