gpt4 book ai didi

cocoa-touch - 将元数据写入图像时出现问题

转载 作者:行者123 更新时间:2023-12-01 08:18:45 25 4
gpt4 key购买 nike

我正在使用 AvFoundation 拍摄静止图像并将 GPS 信息添加到元数据并使用 Assets 库保存到相册,但 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)
{

}

}];

最佳答案

我遇到了完全相同的问题。我认为关于这个主题的文档不是很好,所以我最后通过查看相机应用程序拍摄的照片的元数据并尝试复制它来解决它。

以下是相机应用程序保存的属性的简要说明:

  • kCGImagePropertyGPSLatitude
    (NSNumber) (十进制格式的纬度)
  • kCGImagePropertyGPSLongitude
    (NSNumber) (十进制格式的经度)
  • kCGImagePropertyGPSLatitudeRef
    (NSString) (N 或 S)
  • kCGImagePropertyGPSLongitudeRef
    (NSString) (E 或 W)
  • kCGImagePropertyGPSTimeStamp
    (NSString) (格式为 04:30:51.71
    (UTC 时间戳))

  • 如果你坚持这些,你应该没问题。这是一个示例:
    CFDictionaryRef metaDict = CMCopyDictionaryOfAttachments(NULL, imageDataSampleBuffer, kCMAttachmentMode_ShouldPropagate);
    CFMutableDictionaryRef mutable = CFDictionaryCreateMutableCopy(NULL, 0, metaDict);

    NSDictionary *gpsDict = [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];

    CFDictionarySetValue(mutable, kCGImagePropertyGPSDictionary, gpsDict);

    // Get the image
    NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
    UIImage *image = [[UIImage alloc] initWithData:imageData];

    // Get the assets library
    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
    [library writeImageToSavedPhotosAlbum:[image CGImage] metadata:mutable completionBlock:captureComplete];

    这一切都在你的 AVCaptureConnection 对象的 captureStillImageAsynchronouslyFromConnection 方法的 completionHandler 中,而 self.currentLocation 只是一个 CLLocation。我对示例的时间戳和 Lat/Lng Refs 进行了硬编码,以保持简单。

    希望这可以帮助!

    关于cocoa-touch - 将元数据写入图像时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4043685/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com