将元数据写入图像时出现问题
我正在使用 AvFoundation 拍摄静态图像,并将 GPS 信息添加到元数据中,并使用资源库保存到相册,但 GPS 信息根本不保存。
这是我的代码...
[self.stillImageTaker captureStillImageAsynchronouslyFromConnection:videoConnection
completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error)
{
if (imageDataSampleBuffer != NULL)
{
CFDictionaryRef exifAttachments = CMGetAttachment(imageDataSampleBuffer,kCGImagePropertyExifDictionary, NULL);
CFDictionaryRef metadataDict = CMCopyDictionaryOfAttachments(NULL, imageDataSampleBuffer, kCMAttachmentMode_ShouldPropagate);
NSDictionary *gpsDict = [NSDictionary dictionaryWithObjectsAndKeys:@"1",kCGImagePropertyGPSVersion,
@"78.4852",kCGImagePropertyGPSLatitude,@"32.1456",kCGImagePropertyGPSLongitude, nil];
CMSetAttachment(imageDataSampleBuffer,kCGImagePropertyGPSDictionary,gpsDict,kCMAttachmentMode_ShouldPropagate);
CFDictionaryRef newMetadata = CMCopyDictionaryOfAttachments(NULL, imageDataSampleBuffer, kCMAttachmentMode_ShouldPropagate);
CFDictionaryRef gpsAttachments = CMGetAttachment(imageDataSampleBuffer,kCGImagePropertyGPSDictionary, NULL);
if (exifAttachments)
{ // Attachments may be read or additional ones written
}
NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
UIImage *image = [[UIImage alloc] initWithData:imageData];
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
/
NSDictionary *newDict = (NSDictionary *)newMetadata;
[library writeImageToSavedPhotosAlbum:[image CGImage]
metadata:newDict completionBlock:^(NSURL *assetURL, NSError *error)
{
if (error)
{
}
}];
[library release];
[image release];
CFRelease(metadataDict);
CFRelease(newMetadata);
}
else if (error)
{
}
}];
I am using AvFoundation to take still image and adding gps info to metadata and saving to a photo album using Asset library but gps info is not saving at all.
here is my code...
[self.stillImageTaker captureStillImageAsynchronouslyFromConnection:videoConnection
completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error)
{
if (imageDataSampleBuffer != NULL)
{
CFDictionaryRef exifAttachments = CMGetAttachment(imageDataSampleBuffer,kCGImagePropertyExifDictionary, NULL);
CFDictionaryRef metadataDict = CMCopyDictionaryOfAttachments(NULL, imageDataSampleBuffer, kCMAttachmentMode_ShouldPropagate);
NSDictionary *gpsDict = [NSDictionary dictionaryWithObjectsAndKeys:@"1",kCGImagePropertyGPSVersion,
@"78.4852",kCGImagePropertyGPSLatitude,@"32.1456",kCGImagePropertyGPSLongitude, nil];
CMSetAttachment(imageDataSampleBuffer,kCGImagePropertyGPSDictionary,gpsDict,kCMAttachmentMode_ShouldPropagate);
CFDictionaryRef newMetadata = CMCopyDictionaryOfAttachments(NULL, imageDataSampleBuffer, kCMAttachmentMode_ShouldPropagate);
CFDictionaryRef gpsAttachments = CMGetAttachment(imageDataSampleBuffer,kCGImagePropertyGPSDictionary, NULL);
if (exifAttachments)
{ // Attachments may be read or additional ones written
}
NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
UIImage *image = [[UIImage alloc] initWithData:imageData];
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
/
NSDictionary *newDict = (NSDictionary *)newMetadata;
[library writeImageToSavedPhotosAlbum:[image CGImage]
metadata:newDict completionBlock:^(NSURL *assetURL, NSError *error)
{
if (error)
{
}
}];
[library release];
[image release];
CFRelease(metadataDict);
CFRelease(newMetadata);
}
else if (error)
{
}
}];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我有完全相同的问题。我认为关于这个主题的文档不是很好,所以我最终通过查看相机应用程序拍摄的照片的元数据并尝试复制它来解决它。
以下是相机应用程序保存的属性的概述:
(NSNumber) (十进制格式的纬度)
(NSNumber) (十进制格式的经度)
(NSString) (N 或 S)
(NSString) (E 或 W)
(NSString) (格式为 04:30:51.71
(UTC 时间戳))
如果你坚持这些,你应该没问题。这是一个示例:
这一切都在 AVCaptureConnection 对象的 captureStillImageAsynchronouslyFromConnection 方法的completionHandler 中,而 self.currentLocation 只是一个 CLLocation。为了简单起见,我对示例的时间戳和纬度/经度参考进行了硬编码。
希望这有帮助!
I had exactly the same problem. I think the documentation on this topic isn't great, so I solved it in the end by looking at the metadata of a photo taken by the Camera app and trying to replicate it.
Here's a run down of the properties the Camera app saves:
(NSNumber) (Latitude in decimal format)
(NSNumber) (Longitude in decimal format)
(NSString) (Either N or S)
(NSString) (Either E or W)
(NSString) (Of the format 04:30:51.71
(UTC timestamp))
If you stick to these you should be fine. Here's a sample:
This is all in the completionHandler of the captureStillImageAsynchronouslyFromConnection method of your AVCaptureConnection object, and self.currentLocation is just a CLLocation. I hardcoded the timestamp and Lat/Lng Refs for the example to keep things simple.
Hope this helps!
梅森的回答对我很有帮助。您需要进行一些修改,例如设置经度和纬度的绝对值。纬度。下面是使用 CoreLocation + Image I/O 将带有 GPS 信息的 UIImage 写入磁盘的代码片段:
Mason's answer really helped me. You'll need some modifications such as setting the absolute value of longitude & latitude. Here's a code snippet of using CoreLocation + Image I/O to write an UIImage to disk with GPS information:
上面和其他地方有几个答案,可以满足您的大部分需求。这是我从所有这些来源生成的工作代码,它似乎工作得很好。
这一切都是在 captureStillImageAsynchronouslyFromConnection 块内完成的。我感谢所有不同的贡献者:
一如既往,我担心我的版本是否正确。这是结果的 jhead(1):
There are several answers above and elsewhere on SO that give most of what you want. Here is the working code I generated from all of these sources, and it seems to work just fine.
This is all done within the captureStillImageAsynchronouslyFromConnection block. My thanks to all the various contributors:
As always, I worry if I am getting the releases right. Here's a jhead(1) of the result: