How to allow user to pick the image with Swift?

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

    Incase if you don't want to have a separate button, here is a another way. Attached a gesture on imageView itself, where on tap of image a alert will popup with two option. You will have the option to choose either from gallery/photo library or to cancel the alert.

    import UIKit
    import CoreData
    
    class AddDetailsViewController: UIViewController, UITextFieldDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    
    @IBOutlet weak var imageView: UIImageView!
    
    var picker:UIImagePickerController? = UIImagePickerController()
    
    @IBAction func saveButton(sender: AnyObject) {
        let managedContext = (UIApplication.sharedApplication().delegate as? AppDelegate)!.managedObjectContext
    
        let entity = NSEntityDescription.entityForName("Person", inManagedObjectContext: managedContext)
    
        let person = Person(entity: entity!, insertIntoManagedObjectContext: managedContext)
    
        person.image = UIImageJPEGRepresentation(imageView.image!, 1.0) //imageView.image
    
        do {
             try person.managedObjectContext?.save()
             //people.append(person)
           } catch let error as NSError {
             print("Could not save \(error)")
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(AddDetailsViewController.tapGesture(_:)))
        imageView.addGestureRecognizer(tapGesture)
        imageView.userInteractionEnabled = true
    
        picker?.delegate = self
        // Do any additional setup after loading the view.
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func tapGesture(gesture: UIGestureRecognizer) {
        let alert:UIAlertController = UIAlertController(title: "Profile Picture Options", message: nil, preferredStyle: UIAlertControllerStyle.ActionSheet)
    
        let gallaryAction = UIAlertAction(title: "Open Gallary", style: UIAlertActionStyle.Default) {
            UIAlertAction in self.openGallary()
        }
    
        let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel) {
            UIAlertAction in self.cancel()
        }
    
        alert.addAction(gallaryAction)
        alert.addAction(cancelAction)
    
        self.presentViewController(alert, animated: true, completion: nil)
    
    }
    
    
    func openGallary() {
        picker!.allowsEditing = false
        picker!.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
        presentViewController(picker!, animated: true, completion: nil)
    }
    
    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
        if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
            imageView.contentMode = .ScaleAspectFit
            imageView.image = pickedImage
        }
    
        dismissViewControllerAnimated(true, completion: nil)
    }
    
    func cancel(){
        print("Cancel Clicked")
    }
    
    }
    

    Adding more to the question, implemented the logic to store images in CoreData.

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

    Complete copy-paste working image picker for swift 4 based on @user3182143 answer:

    import Foundation
    import UIKit
    
    
    class ImagePickerManager: NSObject, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    
        var picker = UIImagePickerController();
        var alert = UIAlertController(title: "Choose Image", message: nil, preferredStyle: .actionSheet)
        var viewController: UIViewController?
        var pickImageCallback : ((UIImage) -> ())?;
        
        override init(){
            super.init()
            let cameraAction = UIAlertAction(title: "Camera", style: .default){
                UIAlertAction in
                self.openCamera()
            }
            let galleryAction = UIAlertAction(title: "Gallery", style: .default){
                UIAlertAction in
                self.openGallery()
            }
            let cancelAction = UIAlertAction(title: "Cancel", style: .cancel){
                UIAlertAction in
            }
    
            // Add the actions
            picker.delegate = self
            alert.addAction(cameraAction)
            alert.addAction(galleryAction)
            alert.addAction(cancelAction)
        }
    
        func pickImage(_ viewController: UIViewController, _ callback: @escaping ((UIImage) -> ())) {
            pickImageCallback = callback;
            self.viewController = viewController;
    
            alert.popoverPresentationController?.sourceView = self.viewController!.view
    
            viewController.present(alert, animated: true, completion: nil)
        }
        func openCamera(){
            alert.dismiss(animated: true, completion: nil)
            if(UIImagePickerController .isSourceTypeAvailable(.camera)){
                picker.sourceType = .camera
                self.viewController!.present(picker, animated: true, completion: nil)
            } else {
                let alertWarning = UIAlertView(title:"Warning", message: "You don't have camera", delegate:nil, cancelButtonTitle:"OK", otherButtonTitles:"")
                alertWarning.show()
            }
        }
        func openGallery(){
            alert.dismiss(animated: true, completion: nil)
            picker.sourceType = .photoLibrary
            self.viewController!.present(picker, animated: true, completion: nil)
        }
    
        
        func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
            picker.dismiss(animated: true, completion: nil)
        }
        //for swift below 4.2
        //func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        //    picker.dismiss(animated: true, completion: nil)
        //    let image = info[UIImagePickerControllerOriginalImage] as! UIImage
        //    pickImageCallback?(image)
        //}
        
        // For Swift 4.2+
        func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
            picker.dismiss(animated: true, completion: nil)
            guard let image = info[.originalImage] as? UIImage else {
                fatalError("Expected a dictionary containing an image, but was provided the following: \(info)")
            }
            pickImageCallback?(image)
        }
    
    
    
        @objc func imagePickerController(_ picker: UIImagePickerController, pickedImage: UIImage?) {
        }
    
    }
    

    Call it from your viewcontroller like this:

        ImagePickerManager().pickImage(self){ image in
            //here is the image
        }
    

    Also don't forget to include the following keys in your info.plist:

    <key>NSCameraUsageDescription</key>
    <string>This app requires access to the camera.</string>
    <key>NSPhotoLibraryUsageDescription</key>
    <string>This app requires access to the photo library.</string>
    
    0 讨论(0)
  • 2020-12-02 06:50

    For Swift 3:

    1. First, you need to add the following key in info.plist:

          <key>NSPhotoLibraryUsageDescription</key>
      <string>This app requires access to the photo library.</string>
      
    2. Your View controller needs to conform to the following protocols: UIImagePickerControllerDelegate, UINavigationControllerDelegate:

      class ImagePickerViewController:  UIViewController,UIImagePickerControllerDelegate, UINavigationControllerDelegate {}
      
    3. You need to declare the UIImage you will be useing to bind the returned/selected image:

      @IBOutlet weak var myImageView: UIImageView!
      @IBoutlet weak var upLoadImageBtn:UIImage!
      let imagePicker = UIImagePickerController()
      
    4. Set the pickerImage delegate to be your ViewController:

      imagePicker.delegate = self
      
    5. For the upload button, you will need to link to the following image in order to fire the action and display the image picker:

      @IBAction func upLoadImageBtnPressed(_ sender: AnyObject) {
          imagePicker.allowsEditing = false
          imagePicker.sourceType = .photoLibrary
      
      
          /*
          The sourceType property wants a value of the enum named        UIImagePickerControllerSourceType, which gives 3 options:
      
          UIImagePickerControllerSourceType.PhotoLibrary
          UIImagePickerControllerSourceType.Camera
          UIImagePickerControllerSourceType.SavedPhotosAlbum
      
          */
          present(imagePicker, animated: true, completion: nil)
      
      }
      
    6. Your View controller needs to implement the delegate methods for the image picker delegates:

      // MARK: - ImagePicker Delegate
      
      func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
      
          if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
              myImageView.contentMode = .scaleAspectFit
              myImageView.image = pickedImage
          }
      
      
          /*
      
          Swift Dictionary named “info”.  
          We have to unpack it from there with a key asking for what media information we want.
          We just want the image, so that is what we ask for.  For reference, the available options are:
      
          UIImagePickerControllerMediaType
          UIImagePickerControllerOriginalImage
          UIImagePickerControllerEditedImage
          UIImagePickerControllerCropRect
          UIImagePickerControllerMediaURL
          UIImagePickerControllerReferenceURL
          UIImagePickerControllerMediaMetadata
      
          */
          dismiss(animated: true, completion: nil)
      }
      
      func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
          dismiss(animated: true, completion:nil)
      }
      
    0 讨论(0)
提交回复
热议问题