如何使用objective将UIImage中的所有exif数据剥离?我已经能够使用以下方法获得exif数据:
NSData* pngData = UIImagePNGRepresentation(image);
CGImageSourceRef imageSource = CGImageSourceCreateWithData((CFDataRef)pngData, NULL);
NSDictionary* dic = nil;
if ( NULL == imageSource )
{
#ifdef _DEBUG
CGImageSourceStatus status = CGImageSourceGetStatus ( source );
NSLog ( @"Error: file name : %@ - Status: %d", file, status );
#endif
}
else
{
CFDictionaryRef propertyRef = CGImageSourceCopyPropertiesAtIndex ( imageSource, 0, NULL );
CGImageMetadataRef metadataRef = CGImageSourceCopyMetadataAtIndex ( imageSource, 0, NULL );
// CFDictionaryRef metadataRef = CFDictionaryGetValue(imageProperties, kCGImagePropertyExifDictionary);
if (metadataRef) {
NSDictionary* immutableMetadata = (NSDictionary *)metadataRef;
if ( immutableMetadata ) {
dic = [ NSDictionary dictionaryWithDictionary : (NSDictionary *)metadataRef ];
}
CFRelease ( metadataRef );
}
CFRelease(imageSource);
imageSource = nil;
}
return dic;
发布于 2014-08-31 19:27:30
有几个想法:
NSData
或文件内容加载到UIImage
、用UIImagePNGRepresentation
重新提取数据并将其保存回NSData
的过程将从图像中删除元数据。
不过,这种简单的技术也有其缺点。特别要注意的是,强迫它使用PNG表示的过程可能会极大地影响输出NSData
的大小。例如,如果原始图像是压缩的JPEG (例如由摄像机捕获),则生成的PNG文件实际上可以大于原始的JPEG。
通常,我倾向于获取原始数据(如果文档或包中的文件直接加载到NSData
;如果是从ALAssetsLibrary
检索的东西,则检索ALAsset
,以及从ALAssetRepresentation
中检索,然后使用getBytes
获取原始资产的原始二进制表示)。这样就避免了通过UIImage
(特别是在随后使用UIImagePNGRepresentation
或UIImageJEPGRepresentation
)进行往返操作。- Create an image source from the original `NSData`;
- Create an image destination, using the same "number of images" (almost always 1) and the "type";
- When copying the images from the source to the destination, tell it that the appropriate keys (`kCGImagePropertyExifDictionary` and `kCGImagePropertyGPSDictionary` should be set to `kCFNull`), which according to the `CGImageDestinationAddImageFromSource` documentation is how you specify that those should be removed in the destination if they happened to appear in the source).
因此,它看起来可能如下:
kCGImagePropertyGPSDictionary
字典中删除removeExifProperties
条目。CGImageMetadataRef
转换为NSDictionary
。如果您的技术有效,这很好,但我认为CGImageMetadataRef
被认为是一种不透明的数据类型,而实际上应该使用CGImageMetadataCopyTags
提取标记数组:发布于 2014-08-31 05:42:41
根据图像I/O编程指南,您可以使用CGImageDestinationSetProperties
添加属性的CFDictionaryRef
。
它们的示例代码是:
float compression = 1.0; // Lossless compression if available.
int orientation = 4; // Origin is at bottom, left.
CFStringRef myKeys[3];
CFTypeRef myValues[3];
CFDictionaryRef myOptions = NULL;
myKeys[0] = kCGImagePropertyOrientation;
myValues[0] = CFNumberCreate(NULL, kCFNumberIntType, &orientation);
myKeys[1] = kCGImagePropertyHasAlpha;
myValues[1] = kCFBooleanTrue;
myKeys[2] = kCGImageDestinationLossyCompressionQuality;
myValues[2] = CFNumberCreate(NULL, kCFNumberFloatType, &compression);
myOptions = CFDictionaryCreate( NULL, (const void **)myKeys, (const void **)myValues, 3,
&kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
// Release the CFNumber and CFDictionary objects when you no longer need them.
我不明白他们为什么不一路上拿走它。我猜接下来会是:
CFImageDestinationRef
从文档中看,如何执行第一步并不是很清楚。因此,我查看了参考文献,它看起来可以这样做(没有测试):
NSMutableData* mutableData = [[NSMutableData alloc] initWithCapacity:[pngData length]];
CGImageDestinationRef imageDestination = CGImageDestinationCreateWithData((__bridge_transfer CFMutableDataRef)mutableData, <# Some UTI Type #>, 1, NULL);
CGImageDestinationAddImage(imageDestination, image, yourProperties);
CGImageDestinationFinalize(imageDestination);
因此,按照Apple在文档中显示的方式创建您的属性字典,然后创建一个图像目的地并将所有内容写入其中,包括您的属性。之后,您可以访问NSMutableData
对象并读取图像数据。
https://stackoverflow.com/questions/25589118
复制相似问题