如何将图像转换为另一个ViewController并在ImageView中显示? [英] How do I segue an image to another ViewController and display it within an ImageView?

查看:95
本文介绍了如何将图像转换为另一个ViewController并在ImageView中显示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

The following code allows the user to select an image from the gallery or to take a picture and displays it within the ViewController via ImageView. What I want to do here is to pass the image that was taken or selected from the gallery and pass it to another ViewController to which it gets displayed in an ImageView. Ideally once the picture is selected or taken the ViewController changes immediately to the Second ViewController and displays the image in the ImageView.

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

How can this be achieved? thanks.

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

Here is my second 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



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

Edit 2

Given that you have too many mistakes in your code, I'm going to a bit more specific.

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

First: You need to understand the difference between UIImage and UIImageView

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

Second: If you want to perform segue after the user selects an image, you should call the method on the didFinishPickingMediaWithInfo delegate, like this:

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,但您可以稍后更改)。

Also, I added a new property called "image" (should be called "selectedImage", but you can change it later).

private var image: UIImage? // THIS ONE

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

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

Third: On the ViewController2 you need to set the image library to the UIImageView.

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

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

Finally: In the Storyboard, select ViewController, go to editor -> Embed In -> Navigation Controller.

现在,应该可以正常工作。

Now, should work just fine.



选择图像后,请致电以下方法:


Once you select the image, call the following method:

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。

Keep in mind that for this to work, you should have public property on NextViewController (just like Unis Barakat said). Then on the NextViewController viewDidLoad() you could set the image to the imageView.

希望这会有所帮助!

这篇关于如何将图像转换为另一个ViewController并在ImageView中显示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆