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

标签 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);
         }
     }
     ];
}

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

如何捕捉静止图像并保存其位置名称以显示在照片应用程序中?

感谢您的帮助!

最佳答案

来自文档:captureStillImageAsynchronouslyFromConnection

handler

A block to invoke after the image has been captured. The block parameters are as follows: imageDataSampleBuffer The data that was captured. The buffer attachments may contain metadata appropriate to the image data format. For example, a buffer containing JPEG data may carry a kCGImagePropertyExifDictionary as an attachment. See ImageIO/CGImageProperties.h for a list of keys and value types.

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

         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));

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

[library writeImageToSavedPhotosAlbum:[image CGImage] metadata:mutable completionBlock: nil];

关于ios - 如何使用 AV Foundation 相机拍摄静止图像并保存城市名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19447479/

相关文章:

ios - Swift:不小心将一个导出添加到两个不同的文件,删除了两个导出

ios - 如何删除所有导航栏后退按钮标题

swift - 从 CLLocationManager (Google Maps API) 获取用户位置的问题

ios - 使用SCRecorder时访问权限错误

ios - 如何使用 AVFoundation 翻转视频

ios - 在指定时间间隔使用 CoreLocation 在后台检索 CoreMotion 数据

ios - 打开应用程序时获取当前位置

ios - 将信息保存在数组中

ios - 如何使用 Xcode 调试器打印出属性的内容?

iOS:有选择地禁用 captureOutput 的处理:didOutputSampleBuffer:fromConnection:来自 AVFoundation 中的主线程