簡體   English   中英

如何快速從 UIImagePickerController 獲取 UIImage 元數據? 像“.location”

[英]How to get UIImage metadata from UIImagePickerController in swift ? like ".location"

這是我嘗試過的代碼

public func imagePickerController(_ picker: UIImagePickerController,
                                  didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) {
    guard let image = info[.editedImage] as? UIImage else {
        return self.pickerController(picker, didSelect: nil)
    }
    if let asset: PHAsset = info[UIImagePickerController.InfoKey.phAsset] as? PHAsset {
        print("Asset: \(asset)")
        print("Creation Data \(String(describing: asset.creationDate))")
        print("Location: \(String(describing: asset.location))")
        print("burstIdentifier: \(String(describing: asset.burstIdentifier))")
        print("burstSelectionTypes: \(String(describing: asset.burstSelectionTypes))")
        print("duration: \(String(describing: asset.duration))")
        print("mediaSubtypes: \(String(describing: asset.mediaSubtypes))")
        print("mediaType: \(String(describing: asset.mediaType))")
        print("pixelHeight: \(String(describing: asset.pixelHeight))")
        print("pixelWidth: \(String(describing: asset.pixelWidth))")
        print("sourceType: \(String(describing: asset.sourceType))")
    } else {
        print("Asset: nil")
    }
    self.pickerController(picker, didSelect: image)
}

注意:我也嘗試過使用 PHAsset,但它總是返回 nil。 還可以找到一些建議,但所有建議都已棄用。

您需要先將圖像存儲在 PHAsset 中,然后您將獲得資產對象。

if let originalImage = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
        self.getAssetAfterStoreImage(image: originalImage) { (assetInstance) in
            //asset
            print(assetInstance)
        }
    }
}

func getAssetAfterStoreImage(image:UIImage,completion:@escaping (PHAsset) -> Void){
    
    PHPhotoLibrary.shared().performChanges({
        
        PHAssetChangeRequest.creationRequestForAsset(from: image)
    }) { saved, error in
        
        if saved {
            let fetchOptions = PHFetchOptions()
            fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: true)]
            
            // After uploading we fetch the PHAsset for most recent video and then get its current location url
            if let fetchResult = PHAsset.fetchAssets(with:.image, options: fetchOptions).lastObject{
                completion(fetchResult)
            }
        }
    }
}

如果有任何疑問,請告訴我。

你的代碼沒問題。 只需要授予相機應用程序的位置權限
這是授予許可的步驟。 設置應用程序 -> 隱私 -> 定位服務(第一個選項)-> 相機 ->使用應用程序時選擇選項

你的代碼輸出

Asset: <PHAsset: 0x114d25100> BE7D9140-3D8D-4EAC-8D1F-F97991A9E1EF/L0/001 mediaType=1/0, sourceType=1, (2448x3264), creationDate=2020-10-22 06:26:50 +0000, location=1, hidden=0, favorite=0 
Creation Data Optional(2020-10-22 06:26:50 +0000)
Location: Optional(<+23.043114450,+72.6401118833> +/- 0.00m (speed 0.00 mps / course 95.04) @ 01/01/01, 5:30:00 AM India Standard Time)
burstIdentifier: nil
burstSelectionTypes: PHAssetBurstSelectionType(rawValue: 0)
duration: 0.0
mediaSubtypes: PHAssetMediaSubtype(rawValue: 0)
mediaType: PHAssetMediaType
pixelHeight: 3264
pixelWidth: 2448
sourceType: PHAssetSourceType(rawValue: 1)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM