Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting the image taken from the gallery to the UIImageView

The problem am facing is placing the picture i choose from the gallery into theUIImageView (imageChosen).

The code runs fine without any errors but the picture i chose is not set to the "imageChosen"

here is my code

class postChoices: UIViewController {

    @IBOutlet weak var imageChosen: UIImageView!

    @IBAction func gallery(sender: AnyObject) {

        var image = UIImagePickerController()
        //image.delegate = self
        image.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
        image.allowsEditing = false

        self.presentViewController(image, animated: true, completion: nil)

    }
    override func viewDidLoad() {
        super.viewDidLoad()

            // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func imagePickerController(picker: UIImagePickerController!, didFinishPickingImage image2: UIImageView!, editingInfo: NSDictionary!) {
      //  let selectedImage : UIImageView = image
        imageChosen.image = image2.image

    }


}
like image 737
Aryam Saleh Avatar asked May 19 '15 10:05

Aryam Saleh


People also ask

How do I add an image to UIImageView?

Since you have your bgImage assigned and linked as an IBOutlet, there is no need to initialize it as a UIImageView... instead all you need to do is set the image property like bgImage. image = UIImage(named: "afternoon") . After running this code, the image appeared fine since it was already assigned using the outlet.

What happens when you set an imageView image property to nil?

image property will automatically handle that for you (when the new image is assigned). Where setting to nil is useful is where you want to explicitly mark the image as no longer in use, but do not want to remove / delete the UIImageView itself or set a new image. In those cases, setting . image = nil is a good idea.

What is the difference between UIImage and UIImageView?

UIImage contains the data for an image. UIImageView is a custom view meant to display the UIImage .

What is UIImageView in Swift?

A view that displays a single image or a sequence of animated images in your interface.


2 Answers

//Complete solution with delegates and image handling    

     import UIKit

        class ViewController: UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate {

            @IBOutlet weak var myImageView: UIImageView!
            let picker = UIImagePickerController()

            @IBAction func gallery(sender: AnyObject) {

                if UIImagePickerController.availableMediaTypesForSourceType(.PhotoLibrary) != nil {
                    picker.allowsEditing = false
                    picker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
                    presentViewController(picker, animated: true, completion: nil)
                } else {
                    noCamera()
                }

            }
            func noCamera(){
                let alertVC = UIAlertController(title: "No Camera", message: "Sorry, Gallery is not accessible.", preferredStyle: .Alert)
                let okAction = UIAlertAction(title: "OK", style:.Default, handler: nil)
                alertVC.addAction(okAction)
                presentViewController(alertVC, animated: true, completion: nil)
            }

            override func viewDidLoad() {
                super.viewDidLoad()
                // Do any additional setup after loading the view, typically from a nib.
                picker.delegate = self   //the required delegate to get a photo back to the app.
             }

            //MARK: - Delegates
            //What to do when the picker returns with a photo
            func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
                var chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage //2
                myImageView.contentMode = .ScaleAspectFit //3
                myImageView.image = chosenImage //4
                dismissViewControllerAnimated(true, completion: nil) //5
            }
            //What to do if the image picker cancels.
            func imagePickerControllerDidCancel(picker: UIImagePickerController) {
                dismissViewControllerAnimated(true, completion: nil)
            }
        }

Demo Project

like image 138
Muhammad Adnan Avatar answered Oct 13 '22 22:10

Muhammad Adnan


Several problems.

If you look at the docs the method imagePickerController:didFinishPickingImage:editingInfo: was deprecated in iOS 3.0.

It's a fair bet it's not even being called. (You have the line that sets up your view controller as the delegate commented out, so the image picker won't call your delegate methods.

In your imagePickerController:didFinishPickingImage:editingInfo: method you have image2 defined as a UIImageView. It's not, it's a UIImage.

You should un-comment the line image.delegate = self.

You should implement the method imagePickerController:didFinishPickingMediaWithInfo: instead of imagePickerController:didFinishPickingImage:editingInfo:.

You probably also need to add UIImagePickerControllerDelegate to the definition of your view controller class so the compiler knows that your view controller conforms to the UIImagePickerControllerDelegate protocol.

like image 2
Duncan C Avatar answered Oct 13 '22 23:10

Duncan C