0

my code works fine in choosing the picture from the gallery and display it in the same view, what am stuck into now, is transferring that UIImageView chosen to the next activity when "next" button is clicked

here is the code that opens the gallery

@IBAction func gallery(sender: AnyObject) {
    if UIImagePickerController.availableMediaTypesForSourceType(.PhotoLibrary) != nil {
        picker.allowsEditing = false
        picker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
        presentViewController(picker, animated: true, completion: nil)
    }

}

this is the code that displays the image in the same view

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
        var chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage 
        imageChosen.contentMode = .ScaleAspectFit 
        imageChosen.image = chosenImage 
        dismissViewControllerAnimated(true, completion: nil) 

    }

now after the UIImageView saved in the imageChosen, here is the code am using to pass that image to the next view

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    var pass:postView = segue.destinationViewController as! postView
    if(segue.identifier == "next"){
        pass.imgv.image = imageChosen.image
    }
}

this line of code that causes the program to crash

pass.imgv.image = imageChosen.image

the imgv in the second view is declared like this

@IBOutlet weak var imgv: UIImageView!

what am i doing wrong here, please direct me

Aryam Saleh
  • 338
  • 2
  • 12

4 Answers4

2

You cannot set data in a view which is not rendered yet . So pass image to second view and set that Image to ImageView in secondView's viewDidLoad

  override func prepareForSegue(segue: (UIStoryboardSegue!), sender: AnyObject!) {
        if segue.identifier == "next" {
            var pass:second = segue.destinationViewController as! second
            pass.currentImage=myImageView.image;
        }
    }

//Second view

class second: UIViewController {

    @IBOutlet weak var tempImgView: UIImageView!
    var currentImage:UIImage!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        if((currentImage) != nil){
            tempImgView.image=currentImage;
        }
    }

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

}
Muhammad Adnan
  • 2,668
  • 15
  • 27
0

You can't access the UIImageView directly in second View Controller. Instead create variable UIImage in second View Controller and assign your selected UIImage to it. Later in viewdidload of second View Controller set the UIImageView.

First View

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    let pass:postView = segue.destinationViewController as! postView
    if(segue.identifier == "next"){
        pass.tempImage = imageChosen.image
    }
}

Second View

@IBOutlet weak var imgv: UIImageView!
var tempImage:UIImage!

override func viewDidLoad() {
    super.viewDidLoad()
    self.imgv.image=tempImage
}
Mukesh
  • 3,680
  • 1
  • 15
  • 32
  • When i add this line in the second view var tempImage:UImage it tells me, use of undeclared type UIImage – Aryam Saleh May 20 '15 at 07:00
  • do i have to implement an interface or something? – Aryam Saleh May 20 '15 at 07:00
  • sorry, thanks its working, but when i click next, the app crashes – Aryam Saleh May 20 '15 at 07:08
  • when it crashes, it takes me to this line of code pass.tempImage = imageChosen.image and tells me thread 1: breakpoint 2.1 – Aryam Saleh May 20 '15 at 07:11
  • check if if image is not nil or not in condition.Check u have properly connected the segue or not – Mukesh May 20 '15 at 07:17
  • when i checked if the image is nil or not, the still crashed, but the difference is not it takes me to different breakpoint, does it mean that the image is nil? – Aryam Saleh May 20 '15 at 07:22
  • first chck the segue is working or not by commenting the line of setting the image in both the file.If it is proper then `NSLog' the image is it nil or not – Mukesh May 20 '15 at 07:29
0

You are using weak reference for image view and trying to get it in other controllers . do as following .

@IBOutlet Strong var imgv: UIImageView

Secondly your way of fetching view controller wrong ... Try it as following

if segue.identifier == "ShowCounterSegue"
{
    if let destinationVC = segue.destinationViewController as? OtherViewController{
        destinationVC.numberToDisplay = counter
     }
 }
MOHAMMAD ISHAQ
  • 988
  • 7
  • 15
0
  1. In the PostView, just create variable like var img: UIImage!

  2. Replace this line pass.imgv.image = imageChosen.image with pass.img = imageChosen.image

  3. In viewDidLoad() of PostView, add this line, imgv.image = image

We can not set properties of IBOutlets of secondView from the firstView, because IBOutlets of ViewController will get memory after execution of viewDidLoad()

So you have to create appropriate data variable to pass data to IBOutlet.

Here is the example code.

class FirstViewController: UIViewController {

    @IBOutlet weak var imageViewInput: UIImageView!

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

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

        //check the condition for your segue.identifier, if any

        var destination : NewViewContrller! = segue.destinationViewController as NewViewContrller ;

        if let image = imageViewInput?.image {
            destination.outputImage = image;
        }
    }
}


class NewViewContrller: UIViewController {

    @IBOutlet weak var outputImageView: UIImageView!
    var outputImage: UIImage!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        if let image = outputImage {
            outputImageView.image = image;
        }
    }
}
Hitendra Solanki
  • 4,871
  • 2
  • 22
  • 29