【问题标题】:How to capture still image and save with its city name with AV Foundation camera?如何使用 AV Foundation 相机捕获静止图像并使用城市名称保存?
【发布时间】:2013-10-18 10:31:32
【问题描述】:

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

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

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

感谢您的帮助!

【问题讨论】:

标签: ios avfoundation core-location


【解决方案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];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-20
    相关资源
    最近更新 更多