簡體   English   中英

使用本地圖像URL或UIIMage獲取PHAsset?

[英]Get PHAsset using local image URL or UIIMage?

我想通過使用本地圖像URl或UIImage獲得PHAsset。 如何使用URl或UIImage獲得PHAsset?

希望對您有幫助

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

謝謝

暫無
暫無

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

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