Получить PHAsset, используя локальный URL-адрес изображения или UIIMage? - PullRequest
0 голосов
/ 29 октября 2018

Я хочу получить PHAsset с помощью локального изображения URl или UIImage. Как я могу получить PHAsset, используя URl или UIImage?

1 Ответ

0 голосов
/ 29 октября 2018

Я надеюсь, что это поможет вам

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

Спасибо

...