将图像从一个视图控制器传递到另一个视图控制器

时间:2014-06-24 08:03:06

标签: swift

我尝试使用segue将图像从一个视图控制器传递到另一个视图控制器,但图像为空并且无法使用。

这是mainViewController.swift,所有日志都显示正确的值。

func imagePickerController(picker:UIImagePickerController!,didFinishPickingMediaWithInfo info: NSDictionary!){
    var i:UIImage = info.objectForKey(UIImagePickerControllerOriginalImage) as UIImage
    NSLog("%f, %f", i.size.width, i.size.height);

    orignal = i
    NSLog("iamge %@", orignal)
    performSegueWithIdentifier("showPhoto", sender: self)
    dismissModalViewControllerAnimated(false)
}

override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
    var imageEditcontroller:ImageEditViewController = segue.destinationViewController as ImageEditViewController

    imageEditcontroller.editImage = orignal
    NSLog("iamge %@", imageEditcontroller.editImage)
}

这是另一个视图控制器“ImageEditViewController.swift”

import UIKit

class ImageEditViewController: UIViewController {

    @IBOutlet var imagEditView : UIImageView

    var editImage:UIImage=UIImage()

    @IBAction func doDecorateBtn(sender : AnyObject) {
        print("here text chage5")
    }

    @IBAction func doEmailBtn(sender : AnyObject) {
        print("here text chage6")        
    }

    init(coder aDecoder: NSCoder!) {
        super.init(coder: aDecoder)
        NSLog("iamge2 %@", editImage)            
    }

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        NSLog("iamge1 %@", editImage)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the vie
    }

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

因此,请参阅此视图控制器中的日志,var editImage未初始化为正确的值。对IOS来说是新的,感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

我弄明白,问题不在于我的想法,图像可以传递给另一个viewcontroller。问题是由于弃用API。 " dismissModalViewControllerAnimated&#34 ;.更多细节请看这个问题。 dismissModalViewControllerAnimated deprecated

答案 1 :(得分:0)

见我的。 (我使用了uipickerview和委托方法)

    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {

 let pickedimage = info[UIImagePickerControllerOriginalImage] as! UIImage

imageView.image = pickedimage

            if (imageView.image != nil)
            {
                print("image not empty")
picker .dismissViewControllerAnimated(false, completion: nil)

self .performSegueWithIdentifier("seguetoscoutuploadpage", sender: self)
            }
            else
            {
                print("IMAGE VIEW NIL")
            }
        }



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

        if (segue.identifier == "seguetoscoutuploadpage") {

let scout =  segue.destinationViewController as! ScoutImageUpload
     scout.newImage = imageView.image!

        }
    }