2017-09-26 69 views
0

我需要獲取設備照片庫中圖片的位置。爲此,我試圖加載圖像的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類型。

回答

1

您不能將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