我像这样使用 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/