ios - 使用本地镜像 URL 或 UIIMage 获取 PHAsset?

标签 ios swift xcode phasset

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

最佳答案

希望对你有帮助

//第1步:-导入照片

//step2:- 当您呈现 imagepicker Controller 时

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

swift 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)
}

谢谢

关于ios - 使用本地镜像 URL 或 UIIMage 获取 PHAsset?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53039756/

相关文章:

xcode - 如何突出显示 Xcode 中所有出现的相同变量?

ios - Xcode 停止随机识别类类型

ios - 为什么没有 NSCoding 的默认实现?

javascript - 如何制作目标宽度和高度大于 640px 的高质量 PhoneGap 相机图像?

ios - iOS Swift 中的碰撞检测

ios - Alamofire 下载到文件系统不起作用

ios - 处理CloudKit错误和CKError

javascript - 滚动错误仅出现在 iPad 上

ios - 如何将 JSON 的值放入 TableView

xcode - 创建 Xcode 项目模板需要了解什么?