【问题标题】:Get PHAsset using local image URL or UIIMage?使用本地图像 URL 或 UIIMage 获取 PHAsset?
【发布时间】:2018-10-29 06:16:56
【问题描述】:

我想通过使用本地图像 URl 或 UIImage 来获取 PHAsset。如何使用 URl 或 UIImage 获取 PHAsset?

【问题讨论】:

  • 什么意思?详细说明您的问题。
  • @staticVoidMan 我将图像存储到文档目录中。我将 photoAsset.localIdentifier 存储到核心数据中以再次获取图像。所以当我将图像存储到文档目录时,我没有 localIdentifier。这就是为什么我想使用 UIImage 获取 PHAsset。
  • 保存在文档中时为什么不使用localIdentifier 命名图像,以便您可以使用相同的标识符进行检索。
  • @PankajJangid 您的意思是您将从您的文档目录中提供一个UIImage 对象,以便从您的iOS 库中获取与PHAsset 相同的图像!?像反向图像搜索??
  • @PankajJangid PhotoKit 中没有 API 可以使用 UIImage 并检查 iOS 库中是否有完全相同的图像。我宁愿建议您存储localIdentifier 并将其链接到您保存在文档目录中的UIImage 的路径。稍后当您生成UIImage 的路径时,请检查您的核心数据是否有相关标识符。

标签: ios swift xcode phasset


【解决方案1】:

希望对你有帮助

//step1:- 导入照片

//step2:- 当你展示 imagepicker 控制器时

    if PHPhotoLibrary.authorizationStatus() == .authorized || PHPhotoLibrary.authorizationStatus() == .authorized{
        PHPhotoLibrary.requestAuthorization { [weak self](_) in
        // Present the UIImagePickerController
        self?.present(self!.imagePicker, animated: true, completion: nil)
    }
}

斯威夫特 4.2

extension ViewController:UIImagePickerControllerDelegate,UINavigationControllerDelegate {

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {

        //obtaining saving path
        let fileManager = FileManager.default
        let documentsPath = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first
        let imagePath = documentsPath?.appendingPathComponent("image.jpg")
        print(imagePath ?? "N0 imagePath found")
        // extract image from the picker and save it
        if let pickedImage = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {

            let imageData = pickedImage.jpegData(compressionQuality: 0.75)
            try! imageData?.write(to: imagePath!)
        }

        let identifier = (info[UIImagePickerController.InfoKey.phAsset] as? PHAsset)?.localIdentifier
        print(identifier)
        self.dismiss(animated: true, completion: nil)
    }
}

swift 3.0 和 swift4.0

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {

    //obtaining saving path
    let fileManager = FileManager.default
    let documentsPath = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first
    let imagePath = documentsPath?.appendingPathComponent("image.jpg")
    // extract image from the picker and save it
    if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
        let data = UIImageJPEGRepresentation(pickedImage, 0.75)
        data.write(toFile: localPath!, atomically: true)
    }

    let identifier = (info[UIImagePickerControllerPHAsset] as? PHAsset)?. localIdentifier
    print(identifier)
    self.dismiss(animated: true, completion: nil)
}

谢谢

【讨论】:

  • 如果我的文件 url 像:“file:///var/mobile/Media/DCIM/100APPLE/IMG_0123.JPG”,你能指导我如何从中获取 UIIMAGE 吗?谢谢
猜你喜欢
  • 2020-04-04
  • 1970-01-01
  • 1970-01-01
  • 2016-02-24
  • 2014-02-24
  • 2015-08-28
  • 2016-08-30
  • 2019-02-15
相关资源
最近更新 更多