将UIImageView从主视图移动到另一个视图

时间:2015-05-20 06:47:20

标签: ios swift uiimageview pass-data

我的代码在从图库中选择图片并在同一视图中显示时效果很好, 我现在陷入困境的是,当" next"正在将所选择的UIImageView转移到下一个活动。单击按钮

这是打开图库的代码

@IBAction func gallery(sender: AnyObject) {
    if UIImagePickerController.availableMediaTypesForSourceType(.PhotoLibrary) != nil {
        picker.allowsEditing = false
        picker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
        presentViewController(picker, animated: true, completion: nil)
    }

}

这是在同一视图中显示图像的代码

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
        var chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage 
        imageChosen.contentMode = .ScaleAspectFit 
        imageChosen.image = chosenImage 
        dismissViewControllerAnimated(true, completion: nil) 

    }

现在保存在imageChosen中的UIImageView之后,这里是用于将该图像传递到下一个视图的代码

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    var pass:postView = segue.destinationViewController as! postView
    if(segue.identifier == "next"){
        pass.imgv.image = imageChosen.image
    }
}

导致程序崩溃的这行代码

pass.imgv.image = imageChosen.image

第二个视图中的imgv被声明为

@IBOutlet weak var imgv: UIImageView!

我在这里做错了什么,请指导我

4 个答案:

答案 0 :(得分:2)

您无法在尚未渲染的视图中设置数据。因此,将图像传递给第二个视图,并将该图像设置为secondView的viewDidLoad中的ImageView

  override func prepareForSegue(segue: (UIStoryboardSegue!), sender: AnyObject!) {
        if segue.identifier == "next" {
            var pass:second = segue.destinationViewController as! second
            pass.currentImage=myImageView.image;
        }
    }

//第二个视图

class second: UIViewController {

    @IBOutlet weak var tempImgView: UIImageView!
    var currentImage:UIImage!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        if((currentImage) != nil){
            tempImgView.image=currentImage;
        }
    }

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

}

答案 1 :(得分:0)

您无法直接在第二个View Controller中访问UIImageView。而是在第二个View Controller中创建变量UIImage,并将选定的UIImage分配给它。稍后在第二个View Controller的viewdidload中设置UIImageView

第一视图

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    let pass:postView = segue.destinationViewController as! postView
    if(segue.identifier == "next"){
        pass.tempImage = imageChosen.image
    }
}

第二视图

@IBOutlet weak var imgv: UIImageView!
var tempImage:UIImage!

override func viewDidLoad() {
    super.viewDidLoad()
    self.imgv.image=tempImage
}

答案 2 :(得分:0)

您正在使用弱参考图像视图并尝试在其他控制器中获取它。做如下。

@IBOutlet Strong var imgv:UIImageView

其次,您获取视图控制器的方式错误...请尝试以下

if segue.identifier == "ShowCounterSegue"
{
    if let destinationVC = segue.destinationViewController as? OtherViewController{
        destinationVC.numberToDisplay = counter
     }
 }

答案 3 :(得分:0)

  1. 在PostView中,只需创建var img: UIImage!

  2. 之类的变量
  3. 将此行pass.imgv.image = imageChosen.image替换为pass.img = imageChosen.image

  4. 在PostView的viewDidLoad()中,添加以下行imgv.image = image

  5. 我们无法从firstView设置secondView的IBOutlets属性,因为ViewController 的IBOutlets将在执行viewDidLoad()后获得内存

    因此,您必须创建适当的数据变量以将数据传递给IBOutlet。

    以下是示例代码。

    class FirstViewController: UIViewController {
    
        @IBOutlet weak var imageViewInput: UIImageView!
    
        override func viewDidLoad() {
            super.viewDidLoad()
        }
    
        override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    
            //check the condition for your segue.identifier, if any
    
            var destination : NewViewContrller! = segue.destinationViewController as NewViewContrller ;
    
            if let image = imageViewInput?.image {
                destination.outputImage = image;
            }
        }
    }
    
    
    class NewViewContrller: UIViewController {
    
        @IBOutlet weak var outputImageView: UIImageView!
        var outputImage: UIImage!
    
        override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view, typically from a nib.
    
            if let image = outputImage {
                outputImageView.image = image;
            }
        }
    }