ios - 如何在 swift 4 中从 UIImage 读取 exif 数据?

标签 ios swift uiimage core-image

我有一张图片有很多 exif informations .但是当试图用swift读取exif信息时,它显示的exif信息数量有限。

我试过下面的代码:

let data = UIImageJPEGRepresentation(image, 1.0)
let source = CGImageSourceCreateWithData(data! as CFData, nil)
let metadata = (CGImageSourceCopyPropertiesAtIndex(source!, 0, nil))
debugPrint(metadata ?? "nil")

打印出如下结果:

    {
    ColorModel = RGB;
    Depth = 8;
    Orientation = 6;
    PixelHeight = 2448;
    PixelWidth = 3264;
    ProfileName = "sRGB IEC61966-2.1";
    "{Exif}" =     {
        ColorSpace = 1;
        PixelXDimension = 3264;
        PixelYDimension = 2448;
    };
    "{JFIF}" =     {
        DensityUnit = 0;
        JFIFVersion =         (
            1,
            0,
            1
        );
        XDensity = 72;
        YDensity = 72;
    };
    "{TIFF}" =     {
        Orientation = 6;
    };
}

如何从UIImage中读取所有的exif信息?

最佳答案

如果您的图像是使用 avcapturesession 捕获的。下面是提取 exif 数据的代码。

photoFileOutput?.captureStillImageAsynchronously(from: videoConnection, completionHandler: {(sampleBuffer, error) in
                if (sampleBuffer != nil) {
                        let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(sampleBuffer!)
                        let image = self.processPhoto(imageData!)
                        let source: CGImageSource = CGImageSourceCreateWithData((imageData as! CFMutableData), nil)!

                        let metadata = CGImageSourceCopyPropertiesAtIndex(source, 0,nil) as! [String:Any]

                        print("exif data = \(metadata![kCGImagePropertyExifDictionary as String] as? [String : AnyObject]) ")

                        completionHandler(true)
                    } else {
                        completionHandler(false)
                    }
                }

关于ios - 如何在 swift 4 中从 UIImage 读取 exif 数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50582642/

相关文章:

swift - sqlite.swift 数据库表被锁定(代码 :6)

swift - 使用 swift 4 在波轮下查看图像

ios - UITableViewController 和 NSFetchedResultsController 编辑问题

ios - 我们能得到 UILabel 的名字吗?

ios - 如何从代码中关闭 View 的自动布局?

ios - RxSwift、RxCocoa 和 UITableview

ios - 从父 View 中关闭 View Controller

ios - 将 UIImage 转换为 PFFile 会产生巨大的文件

IOS Swift UIImage 裁剪到给定的矩形大小

ios - 如何覆盖 isEqual : for CLBeacon?