ios - 你能将 UIImage exif 数据复制到缩放后的 UIImage 中吗?

标签 ios uiimage exif

当用户拍照时,我当前正在抓取照片:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
   UIImage *image = info[UIImagePickerControllerOriginalImage];

   // create a jpeg
   NSData *jpegData = UIImageJPEGRepresentation(image, 1.0f);

   // write jpeg image to file in app space
   NSString *filePath = 

   // create file path in app space
   [imageData writeToFile:filePath atomically:NO];
}

这很好用,该文件创建为带有 EXIF 数据的 jpeg 格式。

现在我想将图像缩小一点以使其更小。不过,我想保留原始 UIImage 中存在的部分或全部 EXIF 数据,并将其复制到缩放后的图像中。

当前正在缩放图像:

UIImage *scaledImage = [renderer imageWithActions:^(UIGraphicsImageRendererContext*_Nonnull myContext) {
   [image drawInRect:(CGRect) {.origin = CGPointZero, .size = size}];
}];

这会很好地创建缩放图像,但它不包含任何 EXIF 数据。

有没有办法在缩放图像的同时保留原始图像的 EXIF 数据?我可以从原始图像中获取 EXIF 数据并将其复制到缩放后的图像中吗?

此外,我还使用现已弃用的 ALAssetsLibrary 搜索了很多答案。似乎替代方案是 PhotoKit。其中指出:

In iOS and macOS, PhotoKit provides classes that support building photo-editing extensions for the Photos app. In iOS and tvOS, PhotoKit also provides direct access to the photo and video assets managed by the Photos app.

但是,我没有使用照片应用程序,我的图像不是来自本地照片库或 icloud,因为我只想将照片存储在我的私有(private)应用程序空间中。

最佳答案

来自使用 UIImagePickerController 的相机捕获的元数据到达 UIImagePickerControllerMediaMetadata 键下的 info 字典中。可以使用 ImageIO 框架将其复制到另一个 UIImage 的数据中(您需要导入 ImageIO)。我的代码是 Swift,但它使用了 Objective-C Cocoa 类和 ImageIO C 函数,因此您应该能够轻松地将它转换为 Objective-C:

let jpeg = im!.jpegData(compressionQuality:1) // im is the new UIImage
let src = CGImageSourceCreateWithData(jpeg as CFData, nil)!
let data = NSMutableData()
let uti = CGImageSourceGetType(src)!
let dest = CGImageDestinationCreateWithData(data as CFMutableData, uti, 1, nil)!
CGImageDestinationAddImageFromSource(dest, src, 0, m) // m is the metadata
CGImageDestinationFinalize(dest)

之后,data 是图像 im 的数据以及捕获的元数据 m

关于ios - 你能将 UIImage exif 数据复制到缩放后的 UIImage 中吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54936818/

相关文章:

iphone - NSOperation mainQueue 问题

arrays - Swift - 无法为索引类型为 NSNumber 的值类型 [String] 下标?

Android 相机在没有 ExIf 的情况下保存照片

php - XAMPP + Perl + PHP。不知道如何运行 Perl 脚本

image - Exif 图片方向

ios - 通过 TestFlight 安装旧版本

ios - WatchKit 核心数据同步

ios - 更改 UIBarButtonItem 标题时,过渡不稳定/闪烁

ios - 标签栏 Controller : executing code before switching view

iphone - 如何从 UIWebView 获取整个图像,包括屏幕外内容