将所选图像从UIImagePicker传递到第二个视图控制器

时间:2015-08-10 21:41:10

标签: ios uiimagepickercontroller

我一直在找一个解决方案但是找不到一个解决方案。我希望能够从UIImagePicker中选择一个图像,并将该图像转到第二视图控制器中的ImageView。我有一个触发AlertView的按钮,可以选择从相机或库中获取照片。如果我希望所选图像转到第一个视图上的图像视图但是我无法使其工作以使其进入第二个视图,它可以正常工作。任何建议都将非常感激。 `

class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate{

        var newImage = UIImage(named: "")
        @IBAction func libraryBtn(sender: AnyObject) {

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


    @IBOutlet var imageView: UIImageView!

    var imagePicker = UIImagePickerController()

    override func viewDidLoad() {
        super.viewDidLoad()
        imageView.image = UIImage(named: "beach.jpg" )
        imagePicker.delegate = self

    }

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


    @IBAction func addButton(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


     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




    }


}

1 个答案:

答案 0 :(得分:0)

我不认识Swift,但我确实知道ObjC,所以我会尝试解释:

2级.h:

@property (nonatomic, strong) UIImageView *myImageViewer;

1级.m:

mySecondViewController.myImageViewer = [UIImageView new];
[mySecondViewController.myImageViewer setImage: newImage];

首先,您需要使ImageView全局能够从其他控制器访问。然后在您选择newImage之后,将内存初始化为myImageViewer并将其提供给您刚刚选择的newImage,然后加载viewController。

希望这是你需要听到的内容?