如何将图像转换为另一个ViewController并在ImageView中显示它?

时间:2016-01-26 17:38:27

标签: swift image imageview segue viewcontroller

以下代码允许用户从图库中选择图像或拍照并通过ImageView在ViewController中显示。我想要做的是传递从库中获取或选择的图像,并将其传递给另一个ViewController,它将在ImageView中显示。理想情况下,一旦选择或拍摄照片,ViewController会立即更改为第二个ViewController,并在ImageView中显示图像。

如何实现这一目标?感谢。

import Foundation
import UIKit
import MobileCoreServices
import AssetsLibrary
import AVFoundation
import SystemConfiguration

class ViewController: UIViewController, UITextViewDelegate, UINavigationControllerDelegate, UIImagePickerControllerDelegate, UITextFieldDelegate, UIPopoverControllerDelegate, UIAlertViewDelegate {

    var controller = UIImagePickerController()
    var assetsLibrary = ALAssetsLibrary()
    var selectedImage = UIImageView ()

    private var image: UIImage? // THIS ONE

    @IBOutlet weak var btnClickMe: UIButton!
    @IBOutlet weak var imageView: UIImageView!

    var picker:UIImagePickerController?=UIImagePickerController()
    var popover:UIPopoverController?=nil

    override func viewDidLoad() {
        super.viewDidLoad()
        picker!.delegate=self

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

    }

    @IBAction func btnImagePickerClicked(sender: AnyObject)
    {
        let alert:UIAlertController=UIAlertController(title: "Choose Image", message: nil, preferredStyle: UIAlertControllerStyle.ActionSheet)

        let cameraAction = UIAlertAction(title: "Camera", style: UIAlertActionStyle.Default)
            {
                UIAlertAction in
                self.openCamera()

        }
        let gallaryAction = UIAlertAction(title: "Gallery", style: UIAlertActionStyle.Default)
            {
                UIAlertAction in
                self.openGallary()
        }
        let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel)
            {
                UIAlertAction in

        }

        // Add the actions
        picker?.delegate = self
        alert.addAction(cameraAction)
        alert.addAction(gallaryAction)
        alert.addAction(cancelAction)
        // Present the controller
        if UIDevice.currentDevice().userInterfaceIdiom == .Phone
        {
            self.presentViewController(alert, animated: true, completion: nil)
        }
        else
        {
            popover=UIPopoverController(contentViewController: alert)
            popover!.presentPopoverFromRect(btnClickMe.frame, inView: self.view, permittedArrowDirections: UIPopoverArrowDirection.Any, animated: true)
        }
    }
    func openCamera()
    {
        if(UIImagePickerController .isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera))
        {
            picker!.sourceType = UIImagePickerControllerSourceType.Camera
            self .presentViewController(picker!, animated: true, completion: nil)
        }
        else
        {
            openGallary()
        }
    }
    func openGallary()
    {
        picker!.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
        if UIDevice.currentDevice().userInterfaceIdiom == .Phone
        {
            self.presentViewController(picker!, animated: true, completion: nil)
        }
        else
        {
            popover=UIPopoverController(contentViewController: picker!)
            popover!.presentPopoverFromRect(btnClickMe.frame, inView: self.view, permittedArrowDirections: UIPopoverArrowDirection.Any, animated: true)
        }
    }
    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject])
    {
        picker .dismissViewControllerAnimated(true, completion: nil)
        imageView.image=info[UIImagePickerControllerOriginalImage] as? UIImage
        image = imageView.image!
        performSegueWithIdentifier("TEST", sender: self)
    }
    func imagePickerControllerDidCancel(picker: UIImagePickerController)
    {
        print("picker cancel.")

    }
}

这是我的第二个ViewController

 import UIKit

class ViewController2: UIViewController {

        var Image = UIImage ()

        @IBOutlet var imageView: UIImageView!

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

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

2 个答案:

答案 0 :(得分:2)

编辑2

鉴于你的代码中有太多错误,我会更具体一点。

首先:你需要了解UIImage和UIImageView之间的区别

第二:如果你想在用户选择图像后执行segue,你应该在didFinishPickingMediaWithInfo委托上调用方法,如下所示:

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject])
{
    picker .dismissViewControllerAnimated(true, completion: nil)
    imageView.image=info[UIImagePickerControllerOriginalImage] as? UIImage
    image = imageView.image!
    performSegueWithIdentifier("mySegueToNextViewController", sender: self)
}

另外,我添加了一个名为“image”的新属性(应该称为“selectedImage”,但您可以稍后更改)。

private var image: UIImage? // THIS ONE

@IBOutlet weak var btnClickMe: UIButton!
@IBOutlet weak var imageView: UIImageView!

第三:在ViewController2上,您需要将图像库设置为UIImageView。

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

最后:在Storyboard中,选择ViewController,转到编辑器 - >嵌入 - >导航控制器。

现在,应该工作得很好。


选择图像后,请调用以下方法:

performSegueWithIdentifier("mySegueToNextViewController", sender: self)

然后你需要实现:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "mySegueToNextViewController" {
        if let nextViewController = segue.destinationViewController as? NextViewController {

            nextViewController.image = selectedImage   
        }
    }

}

请记住,要使其正常工作,您应该在NextViewController上拥有公共属性(就像Unis Barakat所说)。 然后在NextViewController viewDidLoad()上,您可以将图像设置为imageView。

希望这有帮助!

答案 1 :(得分:0)

在类的顶部定义一个变量,如:

var selectedImage: UIImage

然后当您在该视图控制器中设置该图像时,在另一个视图控制器中,您只需显示您在第一个控制器中设置的图像。