The following code allows the user to select an image from the gallery or to take a picture and displays it within the ViewController via ImageView. What I want to do here is to pass the image that was taken or selected from the gallery and pass it to another ViewController to which it gets displayed in an ImageView. Ideally once the picture is selected or taken the ViewController changes immediately to the Second ViewController and displays the image in the ImageView.
How can this be achieved? thanks.
import Foundation
import UIKit
import MobileCoreServices
import AssetsLibrary
import AVFoundation
import SystemConfiguration
class ViewController: UIViewController, UITextViewDelegate, UINavigationControllerDelegate, UIImagePickerControllerDelegate, UITextFieldDelegate, UIPopoverControllerDelegate, UIAlertViewDelegate {
var controller = UIImagePickerController()
var assetsLibrary = ALAssetsLibrary()
var selectedImage = UIImageView ()
private var image: UIImage? // THIS ONE
@IBOutlet weak var btnClickMe: UIButton!
@IBOutlet weak var imageView: UIImageView!
var picker:UIImagePickerController?=UIImagePickerController()
var popover:UIPopoverController?=nil
override func viewDidLoad() {
super.viewDidLoad()
picker!.delegate=self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
@IBAction func btnImagePickerClicked(sender: AnyObject)
{
let alert:UIAlertController=UIAlertController(title: "Choose Image", message: nil, preferredStyle: UIAlertControllerStyle.ActionSheet)
let cameraAction = UIAlertAction(title: "Camera", style: UIAlertActionStyle.Default)
{
UIAlertAction in
self.openCamera()
}
let gallaryAction = UIAlertAction(title: "Gallery", style: UIAlertActionStyle.Default)
{
UIAlertAction in
self.openGallary()
}
let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel)
{
UIAlertAction in
}
// Add the actions
picker?.delegate = self
alert.addAction(cameraAction)
alert.addAction(gallaryAction)
alert.addAction(cancelAction)
// Present the controller
if UIDevice.currentDevice().userInterfaceIdiom == .Phone
{
self.presentViewController(alert, animated: true, completion: nil)
}
else
{
popover=UIPopoverController(contentViewController: alert)
popover!.presentPopoverFromRect(btnClickMe.frame, inView: self.view, permittedArrowDirections: UIPopoverArrowDirection.Any, animated: true)
}
}
func openCamera()
{
if(UIImagePickerController .isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera))
{
picker!.sourceType = UIImagePickerControllerSourceType.Camera
self .presentViewController(picker!, animated: true, completion: nil)
}
else
{
openGallary()
}
}
func openGallary()
{
picker!.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
if UIDevice.currentDevice().userInterfaceIdiom == .Phone
{
self.presentViewController(picker!, animated: true, completion: nil)
}
else
{
popover=UIPopoverController(contentViewController: picker!)
popover!.presentPopoverFromRect(btnClickMe.frame, inView: self.view, permittedArrowDirections: UIPopoverArrowDirection.Any, animated: true)
}
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject])
{
picker .dismissViewControllerAnimated(true, completion: nil)
imageView.image=info[UIImagePickerControllerOriginalImage] as? UIImage
image = imageView.image!
performSegueWithIdentifier("TEST", sender: self)
}
func imagePickerControllerDidCancel(picker: UIImagePickerController)
{
print("picker cancel.")
}
}
Here is my second ViewController
import UIKit
class ViewController2: UIViewController {
var Image = UIImage ()
@IBOutlet var imageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
imageView.image = Image
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
Given that you have too many mistakes in your code, I'm going to a bit more specific.
First: You need to understand the difference between UIImage and UIImageView
Second: If you want to perform segue after the user selects an image, you should call the method on the didFinishPickingMediaWithInfo
delegate, like this:
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject])
{
picker .dismissViewControllerAnimated(true, completion: nil)
imageView.image=info[UIImagePickerControllerOriginalImage] as? UIImage
image = imageView.image!
performSegueWithIdentifier("mySegueToNextViewController", sender: self)
}
Also, I added a new property called "image" (should be called "selectedImage", but you can change it later).
private var image: UIImage? // THIS ONE
@IBOutlet weak var btnClickMe: UIButton!
@IBOutlet weak var imageView: UIImageView!
Third: On the ViewController2 you need to set the image library to the UIImageView.
override func viewDidLoad() {
super.viewDidLoad()
imageView.image = selectedImage
}
Finally: In the Storyboard, select ViewController, go to editor -> Embed In -> Navigation Controller.
Now, should work just fine.
Once you select the image, call the following method:
performSegueWithIdentifier("mySegueToNextViewController", sender: self)
Then you need to implement:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "mySegueToNextViewController" {
if let nextViewController = segue.destinationViewController as? NextViewController {
nextViewController.image = selectedImage
}
}
}
Keep in mind that for this to work, you should have public property on NextViewController (just like Unis Barakat said). Then on the NextViewController viewDidLoad() you could set the image to the imageView.
Hope this helps!
on top of class define a variable, something like:
var selectedImage: UIImage
then when you are in that view controller set that image and in the other view controller, you simply display the image that you set in the first controller.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.