如何捕获静止图像并使用AV基金会相机保存其城市名称



我像这样使用 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);
         }
     }
     ];
}

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

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

感谢您的帮助!

来自文档 : captureStillImageAsynclyFromConnection

处理器

捕获映像后要调用的块。该块 参数如下: 图像数据样本缓冲区 捕获。缓冲区附件可能包含适用于 图像数据格式。例如,包含 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];

最新更新