0

I have added a camera & photo library to my app, but what i'm trying to do is when someone selects the camera, takes a photo and chooses it, I want it to direct the user to a new viewcontroller and display the image there and not on the current view where the button is?

So far from seching on google, I have managed to get the new view to load when the camera or library is dismissed, but now I cant seem to figure out how to import the photo too the new view too?!

If anyone can help, it would be great.

Heres my current code below:

    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.
    }









    @IBAction func Camera(sender: AnyObject) {

        if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera) {
        let imagePicker = UIImagePickerController()
        imagePicker.delegate = self
        imagePicker.sourceType = UIImagePickerControllerSourceType.Camera;
        imagePicker.allowsEditing = false
        self.presentViewController(imagePicker, animated: true, completion: nil)
        }



    }



    @IBAction func Images(sender: AnyObject) {

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


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



    }



    func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image:UIImage, editingInfo: [String : AnyObject]?) {

        self.dismissViewControllerAnimated(true, completion: nil)
        let svc = self.storyboard!.instantiateViewControllerWithIdentifier("imageViewer") as! ImageViewController
        self.presentViewController(svc, animated: true, completion: nil)


    }



















}

4 Answers4

0

Add an optional property to your view controller

class ImageViewController: UIViewController {

    var image : UIImage? = nil 

}


func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image:UIImage, editingInfo: [String : AnyObject]?) {

        self.dismissViewControllerAnimated(true, completion: nil)
        let svc = self.storyboard!.instantiateViewControllerWithIdentifier("imageViewer") as! ImageViewController
        svc.image = image // here
        self.presentViewController(svc, animated: true, completion: nil)

    }
B.S.
  • 21,660
  • 14
  • 87
  • 109
  • Hi, thanks for your answer. That Class MyViewController... does that correspond to my ImageViewController? –  Dec 08 '15 at 18:12
  • And do I put that class bit, in the same file as my code I showed you above or in the second one? –  Dec 08 '15 at 18:27
  • Go to ImageViewController.swift and add image property there – B.S. Dec 08 '15 at 18:33
  • I have done so, but when I run it nothing different seems to be happening. where you have typed "here" is anythign suppost to go there? –  Dec 08 '15 at 18:42
0

Based on the above answer, you're trying to set the image to the image view, when you should be trying to set the image to the image of the image view, So, it should be:

func viewDidLoad() { super.viewDidLoad()

// Because the self.image is an optional var, you need to unwrap it
if let image = self.image {
    self.imageView.image = self.image
} 

}

0

In your ImageViewController, or other receiving ViewController, add:

var selectedImage: UIImage? = nil

In didFinishPickingImage, replace your code with this:

func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image:UIImage, editingInfo: [String : AnyObject]?) {
    self.dismissViewControllerAnimated(true, completion: nil)
    let svc = self.storyboard!.instantiateViewControllerWithIdentifier("imageViewer") as! ImageViewController
    svc.selectedImage = image
    self.presentViewController(svc, animated: true, completion: nil)
}

In the receiving ViewController that svc is referring to, add a method to display the image:

class ImageViewController: UIViewController {

    var selectedImage: UIImage? = nil

    var yourImageView = UIImageView()

    override func viewDidLoad() {
        super.viewDidLoad()
        ...
        yourImageView.frame = view.frame
        yourImageView.image = selectedImage
    }
    ...
}
xoudini
  • 7,001
  • 5
  • 23
  • 37
0

I want to suggest a little modification.

Please have a look at this answer: Presenting a modal view controller immediately after dismissing another

func presentImageViewerWithImage(image: UIImage) {

    let svc = self.storyboard!.instantiateViewControllerWithIdentifier("imageViewer") as! ImageViewController
    svc.image = image  //Need to make an UIImage instance variable in new viewController
    self.presentViewController(svc, animated: true, completion: nil)
}

func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image:UIImage, editingInfo: [String : AnyObject]?) {

    self.dismissViewControllerAnimated(false, completion: {
        self.presentImageViewerWithImage(image)
    })
}

Then in the New View controller Make an UIImageView IBOutlet and show the selected image form picker to that 'ImageView'

@IBOutlet weak var aImageView: UIImageView!

var image : UIImage?

override func viewDidLoad() {
    super.viewDidLoad()

    if let image = image {
        self.aImageView.image = image
    }
}
Community
  • 1
  • 1
Warif Akhand Rishi
  • 23,920
  • 8
  • 80
  • 107