1

I am trying to take a photo, resize it and then tap "Use Photo" and switch over to a new view that displays both, the original image and edited image. My trouble is having the new view display after tapping "Use Photo". With my current code, I can dismiss the ImagePickerController just fine but then it goes back to the main view and then I get a black screen.

    @IBAction func pickerButton(button: UIButton)
{
    var controller : UIImagePickerController = UIImagePickerController();
    controller.delegate = self
    //controller.allowsEditing = true
    self.presentViewController(controller, animated: true, completion: nil)

}    

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject])
{
    var mediaDictionary : NSDictionary = info as NSDictionary

    println("here it comes")

    var pickerMedia : String = mediaDictionary.objectForKey("UIImagePickerControllerMediaType") as! String
    dump(pickerMedia)

    if (pickerMedia == "public.image"){
        ///////Loading dictionaries for value pairs that don't exist as of video capture video
        var anImage : UIImage = mediaDictionary.objectForKey("UIImagePickerControllerOriginalImage") as! UIImage

        /////////Optional for edited image
        var editedImage : UIImage = mediaDictionary.objectForKey("UIImagePickerControllerEditedImage") as! UIImage

    // Dismiss the image picker         
    picker.dismissViewControllerAnimated(true, completion: nil)
    // Here is my issue...
    var svc : decisionController = decisionController()
    self.presentViewController(svc, animated: true, completion: nil)

    }

My decisionController View is in a separate swift file and it is in my storyboard. It has the class set to decisionController. I would normally create a segue into my next view but I don't have a button to reference to since Im using a generic UIImagePickerController.

Here is my decisionController.swift

import UIKit

class decisionController: UIViewController {

    @IBOutlet var anImageView : UIImageView?
    @IBOutlet var anotherImageView : UIImageView?

    override func viewDidLoad() {

        super.viewDidLoad()

        //anImageView!.image = anImage
        //anotherImageView!.image = editedImage

        // Do any additional setup after loading the view, typically from a nib.
    }
}
Junior
  • 71
  • 1
  • 2
  • 10

1 Answers1

1

If you want to load a scene from your storyboard without using a segue, you can do so by adding a Storyboard ID to the scene in the storyboard and then invoking instantiateViewControllerForIdentifier: on the storyboard.

Assuming that the presenting view controller is in the same storyboard, this code should work after you set the Storyboard ID by selecting the view controller in the storyboard, and filling in a value under Storyboard ID in the identity inspector (command-option-3).

Replace:

// Here is my issue...
var svc : decisionController = decisionController()
self.presentViewController(svc, animated: true, completion: nil)

with:

let svc = self.storyboard!.instantiateViewControllerWithIdentifier("decisionControllerStoryboardID") as! decisionController
self.presentViewController(svc, animated: true, completion: nil)
Charles A.
  • 10,685
  • 1
  • 42
  • 39
  • Thanks, this solved my problem! But now it created a new one. I can't send any variables to my decisionController anymore using `svc.setImages(oldImage, newImage: newImage)` right after the view instantiates. I've defined my function in decisionController.swift as `func setImages(oldIMage : UIImage, newImage : UIImage) { originalImageView!.image = oldIMage editedImageView!.image = newImage }` – Junior Oct 13 '15 at 22:50
  • The view has not been loaded at that point, so `originalImageView` and `editedImageView` are likely `nil`. You could store the images in private properties and set them in `viewDidLoad`. – Charles A. Oct 13 '15 at 22:52
  • Sorry, I meant `oldIMage editedImageView`, not `editedImageView`. Either way, chances are that your UIImageView instances are `nil`. – Charles A. Oct 13 '15 at 22:59
  • Are you talking about he ImageViews or the actual UIImage object? Why would I store the images in a private property, wouldn't that make them less accessible? And the which viewDidLoad method are you talking about, the decisionContorller's or the main ViewControllers? – Junior Oct 13 '15 at 23:14
  • When you instantiate a view controller from the storyboard, it does not load all of the views immediately, it waits until the `view` property is accessed (this is typically at presentation time). As such, any `@IBOutlet` property you may have setup in `decisionController` will have a `nil` value. Put a breakpoint in your `setImages:newImage:` method and you can verify this. – Charles A. Oct 13 '15 at 23:34
  • Okay, so when I check `oldImage(UIImage)` and `newImage(UIImage)`, sure enough the photo that I took and resized are there. But the values for `originalImageView` and `editedImageView` are nil sure enough. So I should set the images in the `viewDidLoad` method of decisionController so it sets the image after the load has finished appearing? – Junior Oct 14 '15 at 00:09
  • 1
    Yes, that's a typical approach. To do so, you'll need to store the images in properties on `decisionController`. You can check if the view is loaded in your `setImages:newImage:` method by checking the `isViewLoaded` property that exists on `UIViewController`. If it is loaded, you can set them immediately. If it is not, you can store them and set them in `viewDidLoad`. – Charles A. Oct 14 '15 at 17:25
  • Cool! Thanks! It worked out by setting the property of decisionController and discarding the function I created. – Junior Oct 15 '15 at 17:37