How to allow user to pick the image with Swift?

后端 未结 21 839
[愿得一人]
[愿得一人] 2020-12-02 06:01

I am writing my first iOS application (iPhone only) with Swift. The main application view should allow user to choose the image from the photo gallery.

I\'ve found

21条回答
  •  借酒劲吻你
    2020-12-02 06:25

    XCODE 10.1 / SWIFT 4.2 :

    1. Add required permissions (others mentioned)

    2. Add this class to your view:


        import UIKit
    
        import Photos
    
        import Foundation
    
    class UploadImageViewController: UIViewController, UIImagePickerControllerDelegate , UINavigationControllerDelegate {
    
            @IBOutlet weak var imgView: UIImageView!
    
            let imagePicker = UIImagePickerController()
    
            override func viewDidLoad() {
    
                super.viewDidLoad()
    
                checkPermission()
    
                imagePicker.delegate = self
                imagePicker.allowsEditing = false
                imagePicker.sourceType = .photoLibrary
            }
    
            @IBAction func btnSetProfileImageClickedCamera(_ sender: UIButton) {
            }
    
            @IBAction func btnSetProfileImageClickedFromGallery(_ sender: UIButton) {
                self.selectPhotoFromGallery()
            }
    
            func selectPhotoFromGallery() {
                self.present(imagePicker, animated: true, completion: nil)
            }
    
            func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
    
                if let pickedImage = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
                        self.imgView.contentMode = .scaleAspectFit
                        self.imgView.image = pickedImage
                    }
    
                dismiss(animated: true, completion: nil)
            }
    
    
            func imagePickerControllerDidCancel(_ picker: UIImagePickerController){
                print("cancel is clicked")
            }
    
    
            func checkPermission() {
                let photoAuthorizationStatus = PHPhotoLibrary.authorizationStatus()
                switch photoAuthorizationStatus {
                case .authorized:
                    print("Access is granted by user")
                case .notDetermined:
                    PHPhotoLibrary.requestAuthorization({
                        (newStatus) in
                        print("status is \(newStatus)")
                        if newStatus ==  PHAuthorizationStatus.authorized {
                            /* do stuff here */
                            print("success")
                        }
                    })
                    print("It is not determined until now")
                case .restricted:
                    // same same
                    print("User do not have access to photo album.")
                case .denied:
                    // same same
                    print("User has denied the permission.")
                }
            }
        }
    

提交回复
热议问题