How to allow user to pick the image with Swift?

后端 未结 21 860
[愿得一人]
[愿得一人] 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:31

    If you just want let the user choose image with UIImagePickerController use this code:

    import UIKit
    
    
    class ViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
    
        @IBOutlet var imageView: UIImageView!
        @IBOutlet var chooseBuuton: UIButton!
        var imagePicker = UIImagePickerController()
    
        @IBAction func btnClicked() {
    
            if UIImagePickerController.isSourceTypeAvailable(.savedPhotosAlbum){
                print("Button capture")
    
                imagePicker.delegate = self
                imagePicker.sourceType = .savedPhotosAlbum
                imagePicker.allowsEditing = false
    
                present(imagePicker, animated: true, completion: nil)
            }
        }
    
        func imagePickerController(picker: UIImagePickerController!, didFinishPickingImage image: UIImage!, editingInfo: NSDictionary!){
            self.dismiss(animated: true, completion: { () -> Void in
    
            })
    
            imageView.image = image
        }
    }
    

提交回复
热议问题