How to allow user to pick the image with Swift?

后端 未结 21 834
[愿得一人]
[愿得一人] 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条回答
  •  Happy的楠姐
    2020-12-02 06:36

    Try this one it's easy.. to Pic a Image using UIImagePickerControllerDelegate

        @objc func masterAction(_ sender: UIButton)
        {
            if UIImagePickerController.isSourceTypeAvailable(.savedPhotosAlbum){
                print("Button capture")
    
                imagePicker.delegate = self
                imagePicker.sourceType = .savedPhotosAlbum;
                imagePicker.allowsEditing = false
    
                self.present(imagePicker, animated: true, completion: nil)
            }
    
            print("hello i'm touch \(sender.tag)")
        }
    
        func imagePickerControllerDidCancel(_ picker:
            UIImagePickerController) {
            dismiss(animated: true, completion: nil)
        }
    
        func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
            if let chosenImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
                print("Get Image \(chosenImage)")
                ImageList.insert(chosenImage, at: 0)
                ArrayList.insert("", at: 0)
                Collection_Vw.reloadData()
            } else{
                print("Something went wrong")
            }
    
            self.dismiss(animated: true, completion: nil)
        }
    

提交回复
热议问题