How to allow user to pick the image with Swift?

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

    click on button and open Image gallery and set image in imageview swift 3.0

    add three delegate UIImagePickerControllerDelegate,UIPopoverControllerDelegate,UINavigationControllerDelegate

    var picker:UIImagePickerController?=UIImagePickerController()
    @IBOutlet var imgPhoto: UIImageView!
    
       override func viewDidLoad() {
        super.viewDidLoad()
        picker?.delegate=self
       }
    
     @IBAction func btnAddPhotoClicked(_ sender: UIButton) {
        openGallary()
       }
    
    func openGallary()
    {
        picker!.allowsEditing = false
        picker!.sourceType = UIImagePickerControllerSourceType.photoLibrary
        present(picker!, animated: true, completion: nil)
    }
    
    //MARK:- ImagePicker Controller Delegate
    //MARK:-
    
    func imagePickerControllerDidCancel(_ picker: 
    UIImagePickerController) {
        dismiss(animated: true, completion: nil)
    }
    
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        if let chosenImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
            imgPhoto.contentMode = .scaleToFill
            imgPhoto.image = chosenImage
        } else{
            print("Something went wrong")
        }
    
        self.dismiss(animated: true, completion: nil)
    }
    

提交回复
热议问题