如何将选定的图像从ImagePickerController传递到第二个视图控制器?

时间:2015-08-13 18:31:01

标签: ios swift xcode6 uiimagepickercontroller uistoryboardsegue

我已经详尽地试图找到这个问题的答案。在我创建的应用程序中,用户将单击addImage按钮,然后从ImagePickerController中选择照片库(或相机)中的图像,然后将所选图像传递到第二个视图控制器上的图像视图。

我知道当图像视图与addImage按钮位于同一视图上时如何执行此操作,但无法弄清楚如何正确使用segue将图像传递到第二个视图。希望有人可以帮我解决这个问题。

import UIKit

class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate{

        var newImage = UIImage(named: "")


    @IBOutlet var imageView: UIImageView!

    var imagePicker = UIImagePickerController()

    override func viewDidLoad() {
        super.viewDidLoad()

        imagePicker.delegate = self

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    @IBAction func addImage(sender: UIButton) {

        var alert = UIAlertController(title: "Please Get An Image", message: "Choose from below", preferredStyle: .Alert)

        alert.addAction(UIAlertAction(title: "Camera", style: .Default, handler: {(action) -> Void in

                        self.imagePicker.sourceType = UIImagePickerControllerSourceType.Camera
                        self.presentViewController(self.imagePicker, animated: true, completion: nil)
        }))

        alert.addAction(UIAlertAction(title: "Photo Library", style: .Default, handler: {(action) -> Void in

                        self.imagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
                        self.presentViewController(self.imagePicker, animated: true, completion: nil)
        }))

        alert.addAction(UIAlertAction(title: "Cancel", style: .Default, handler: {(action) -> Void in

            self.dismissViewControllerAnimated(true, completion: nil)



        }))


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

    }

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

        newImage = image

     //not sure about this next line here...
     performSegueWithIdentifier("VC2", sender: self)


      self.dismissViewControllerAnimated(true, completion: nil)

    }

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

        var secondViewController: VC2 = segue.destinationViewController as! VC2
        secondViewController.imageView.image = newImage




    }


}// end of app

3 个答案:

答案 0 :(得分:0)

OK susan,所以当您在相机的预览部分中单击“使用照片”时,您要实现的位置将转移到第二个VC。

imgView.image = holderImage.image

编辑:在您确认要使用该图像后,上述代码将被删除。

在你的第二个VC中,有一个持有者,然后将持有者图像发送到你的UIImageView。在您的第一个VC中,将图像发送到持有者图像。

你的SecondVC:

a

在viewDidLoad()

var sortiment = [];
var geschmack = [];
var kategorie = [];

function filterOptions(eigenschaft, filter){
    inhalt = filter + " = " + eigenschaft;
    console.log(inhalt);
    console.log(sortiment[0]);
    a = sortiment.indexOf(inhalt);
    console.log(a);

    switch(filter) {
        case "sortiment":
            sortiment.push([inhalt]);
            break;
        case "geschmack":
            geschmack.push([inhalt]);
            break;
        case "kategorie":
            kategorie.push([inhalt]);
            break;
        default:
            console.log("FAIL");
    }
}

答案 1 :(得分:0)

你不应该使用UIImage对象传递UIImageView,因为这不是一个好主意传递这样的图像,而是可以在SecondViewController中创建UIImage对象简单传递{{1对象到 SecondVC 使用这个像这样的图像对象

UIImage

答案 2 :(得分:0)

确保segue的标识符设置为“VC2”。而且,而不是设置

secondViewController.imageView.image = newImage

在你的secondViewController上公开一个获取图像的属性。使用prepareForSegue中的图像设置属性。

secondViewController.image = newImage

然后,在secondViewController上的viewDidLoad中,使用属性中的值设置其imageView。

override func viewDidLoad() {
  super.viewDidLoad()
  imageView.image = image
}

在您的代码中,由于视图生命周期,第二个控制器上的ImageView尚不存在。

您还可以在prepareForSegue中设置断点,以确保正确调用它并正确设置第二个控制器。

相关问题