How to allow user to pick the image with Swift?

后端 未结 21 802
[愿得一人]
[愿得一人] 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.

提交回复
热议问题