iOS Swift无法访问存储的照片上的EXIF数据

问题描述:

我需要获取设备照片库中图片的位置。为此,我试图加载图像的Exif数据。 这是我的代码:iOS Swift无法访问存储的照片上的EXIF数据

import ImageIO 

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { 
     let image = info[UIImagePickerControllerOriginalImage] as? UIImage 
     let ciImage = CIImage(image: image!) 
     let imageProperties = CGImageSourceCopyPropertiesAtIndex(ciImage as! CGImageSource, 0, nil) as Dictionary? 
     if let dict = imageProperties as? [String: Any] { 
     print(dict) 
    } 
    } 

当我尝试到设备中,应用程序崩溃上运行我的代码。 我认为问题是在演员阵容中,我试图做一些操纵,但我无法将UIImage转换为CFImageSource类型。

您不能将CIImage(也不是UIImage)投射到CGImageSource。要创建CGImageSource做:

guard let imageData = UIImageJPEGRepresentation(uiImage, 1) 
    else { fatalError("Error getting image data") } 

guard let imageSource = CGImageSourceCreateWithData(imageData as CFData, nil) 
    else { fatalError("Error creating image source") } 

let properties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, nil) 

UIImageJPEGRepresentation返回图像作为JPEG图像所以也无所谓什么格式uiImage最初被

+1

Thans,它完美 – Szanownego