【问题标题】:How to get UIImage metadata from UIImagePickerController in swift ? like ".location"如何快速从 UIImagePickerController 获取 UIImage 元数据?像“.location”
【发布时间】:2020-10-22 05:48:02
【问题描述】:

这是我尝试过的代码

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。 还可以找到一些建议,但都已弃用。

【问题讨论】:

    标签: ios swift uiimage uiimagepickercontroller


    【解决方案1】:

    您需要先将图像存储在 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)
                }
            }
        }
    }
    

    如有任何疑问,请告诉我。

    【讨论】:

      【解决方案2】:

      您的代码很好。只需为相机应用授予位置权限
      这是授予权限的步骤。 设置应用 -> 隐私 -> 定位服务(第一个选项) -> 相机 -> 选择选项使用应用时

      您的代码输出

      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)
      

      【讨论】:

      • 是的@iParesh,对,但我们还需要在应用程序中设置权限,而不是获得位置。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-04-03
      • 2016-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-08
      • 1970-01-01
      相关资源
      最近更新 更多