【问题标题】:iOS: How to extract thumbnail and metadata from photo file on diskiOS:如何从磁盘上的照片文件中提取缩略图和元数据
【发布时间】:2021-04-05 01:01:25
【问题描述】:

Apple doc here https://developer.apple.com/documentation/avfoundation/cameras_and_media_capture/capturing_still_and_live_photos/capturing_thumbnail_and_preview_images 解释了如何捕获缩略图。

在底部,它说

如果您请求嵌入的缩略图图像,则该图像不是 可以从 AVCapturePhoto 对象直接访问——它嵌入在 您通过调用照片对象的 fileDataRepresentation() 方法。

似乎无法将嵌入的缩略图与主照片分开。那么嵌入式缩略图是什么意思呢?

我想将 JPG 和原始 DNG 格式的 AVCapturePhoto(两者都需要嵌入缩略图)保存到 App 的 Documents 目录(我不使用 PhotoKit),然后将其加载回 UIImageView

我保存这样一张照片:

if let data = capturePhoto.fileDataRepresentation() {
    data.write(to: documentsPath, options: [])
}

然后将其加载回UIImage,如下所示:

if let data = FileManager.default.contents(at: path) {
    let image = UIImage(data: data)
}

但最好先加载嵌入的thubmail。如果用户点击查看大图,则加载完整的图片文件。

我还想显示元数据,例如 GPS 位置、闪光灯状态、ISO、快门速度等。我想知道该怎么做。

【问题讨论】:

    标签: ios avfoundation metadata thumbnails photo


    【解决方案1】:

    AVFoundationAVAsset 包装了一些元数据类型,但显然不是 EXIF 数据。如果你想要缩略图,你必须使用CoreGraphics 框架。此函数获取缩略图(如果存在),将最大边长限制为 512 像素。

    public func getImageThumbnail(url: URL) -> CGImage? {
        guard let imageSource = CGImageSourceCreateWithURL(url as CFURL, nil) else { return nil }
        
        let thumbnailOptions: [String: Any] = [
            kCGImageSourceCreateThumbnailWithTransform as String: true,
            kCGImageSourceCreateThumbnailFromImageIfAbsent as String: false, // true will create if thumbnail not present
            kCGImageSourceThumbnailMaxPixelSize as String: 512
        ]
        return CGImageSourceCreateThumbnailAtIndex(imageSource, 0, thumbnailOptions as CFDictionary);
    }
    

    对于所有其余元数据,您可以使用CGImageSourceCopyPropertiesAtIndexCGImageSourceCopyMetadataAtIndex

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-03-29
      • 1970-01-01
      • 2010-12-06
      • 2012-05-08
      • 1970-01-01
      • 1970-01-01
      • 2012-11-12
      相关资源
      最近更新 更多