moving the UIImageView from the main view to another

北城余情 提交于 2019-12-12 01:26:22

问题


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


回答1:


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()
    }

}



回答2:


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



回答3:


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
}



回答4:


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


来源:https://stackoverflow.com/questions/30342213/moving-the-uiimageview-from-the-main-view-to-another

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!