How to allow user to pick the image with Swift?

后端 未结 21 734
[愿得一人]
[愿得一人] 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)
    }
    
    0 讨论(0)
  • 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)
        }
    
    0 讨论(0)
  • 2020-12-02 06:42

    Just answering here to mention: info[UIImagePickerControllerEditedImage] is probably the one you want to use in most cases.

    Other than that, the answers here are comprehensive.

    0 讨论(0)
  • 2020-12-02 06:42

    Xcode 10, Swift 4.2

    Below is a slightly optimized version of the implementation. This is in Swift 4.2 and I have tested it too.

    You can see the complete code for ViewController here. Please note that you have to define an IBOutlet (imageView) and IBAction (didTapOnChooseImageButton) defined and connected in storyboard too. Hope this helps.

    import UIKit
    
    class ImagePickViewController: UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate {
    
    var imagePicker = UIImagePickerController()
    @IBOutlet weak var imageView: UIImageView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }
    
    @IBAction func didTapOnChooseImageButton(_ sender: Any) {
        let alert:UIAlertController=UIAlertController(title: "Choose Image", message: nil, preferredStyle: UIAlertController.Style.actionSheet)
        let cameraAction = UIAlertAction(title: "Camera", style: UIAlertAction.Style.default) {
            UIAlertAction in
            self.openCamera(UIImagePickerController.SourceType.camera)
        }
        let gallaryAction = UIAlertAction(title: "Gallary", style: UIAlertAction.Style.default) {
            UIAlertAction in
            self.openCamera(UIImagePickerController.SourceType.photoLibrary)
        }
        let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertAction.Style.cancel) {
            UIAlertAction in
        }
    
        // Add the actions
        imagePicker.delegate = self as UIImagePickerControllerDelegate & UINavigationControllerDelegate
        alert.addAction(cameraAction)
        alert.addAction(gallaryAction)
        alert.addAction(cancelAction)
        self.present(alert, animated: true, completion: nil)
    }
    
    func openCamera(_ sourceType: UIImagePickerController.SourceType) {
        imagePicker.sourceType = sourceType
        self.present(imagePicker, animated: true, completion: nil)
    }
    
    //MARK:UIImagePickerControllerDelegate
    
    func imagePickerController(_ picker: UIImagePickerController,
                                        didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        imageView.image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage
        imagePicker.dismiss(animated: true, completion: nil)
    }
    
    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        print("imagePickerController cancel")
    }
    
    }
    
    0 讨论(0)
  • 2020-12-02 06:45

    Do this stuff for displaying photo library images swift coding:

    var pkcrviewUI = UIImagePickerController()
            if UIImagePickerController .isSourceTypeAvailable(UIImagePickerControllerSourceType.PhotoLibrary)
            {
                pkcrviewUI.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
                pkcrviewUI.allowsEditing = true
                pkcrviewUI.delegate = self
                [self .presentViewController(pkcrviewUI, animated: true , completion: nil)]
            }
    
    0 讨论(0)
  • 2020-12-02 06:47

    I know this is question is a year old, but here's some pretty simple code (mostly from this tutorial) that's working well for me:

    import UIKit
    
    class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    
    @IBOutlet weak var imageView: UIImageView!
    
    var imagePicker = UIImagePickerController()
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        self.imagePicker.delegate = self
    }
    
    @IBAction func loadImageButtonTapped(sender: AnyObject) {
        print("hey!")
        self.imagePicker.allowsEditing = false
        self.imagePicker.sourceType = .SavedPhotosAlbum
    
        self.presentViewController(imagePicker, animated: true, completion: nil)
    }
    
    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
        if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
            self.imageView.contentMode = .ScaleAspectFit
            self.imageView.image = pickedImage
        }
    
        dismissViewControllerAnimated(true, completion: nil)
    
    }
    
    func imagePickerControllerDidCancel(picker: UIImagePickerController) {
        self.imagePicker = UIImagePickerController()
        dismissViewControllerAnimated(true, completion: nil)
    }
    
    0 讨论(0)
提交回复
热议问题