如何在选择/拍摄后将图像从相机/相机胶卷传递到新视图?

时间:2015-12-11 17:46:13

标签: ios swift camera uiimagepickercontroller

我添加了相机和相机照片库到我的应用程序,但我想要做的是当有人选择相机,拍照并选择它时,我希望它将用户引导到新的viewcontroller并在那里显示图像而不是当前查看按钮的位置?

我在Google上搜索过但没有找到任何明确或快速的内容,而且由于我不了解ObjC,我不确定我所看到的内容是否正确!

以下是我目前的代码:

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
}

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


@IBAction func Camera(sender: AnyObject) {

    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera) {
    let imagePicker = UIImagePickerController()
    imagePicker.delegate = self
    imagePicker.sourceType = UIImagePickerControllerSourceType.Camera;
    imagePicker.allowsEditing = false
    self.presentViewController(imagePicker, animated: true, completion: nil)
    }
}


@IBAction func Images(sender: AnyObject) {

    let imageLoad = UIImagePickerController()
    imageLoad.delegate = self
    imageLoad.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
    imageLoad.allowsEditing = false


    self.presentViewController(imageLoad, animated: true, completion: nil)
}


func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image:UIImage, editingInfo: [String : AnyObject]?) {

    self.dismissViewControllerAnimated(true, completion: nil)

}

2 个答案:

答案 0 :(得分:1)

您只需将图像分配给新的视图控制器:

func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image:UIImage, editingInfo: [String : AnyObject]?) {

    self.dismissViewControllerAnimated(true, completion: nil)

    let svc = self.storyboard!.instantiateViewControllerWithIdentifier("imageViewer") as! ImageViewController
    svc.image = image

    self.presentViewController(svc, animated: true, completion: nil)   
}

然后,在imageViewerController中:

func viewDidLoad() {
    super.viewDidLoad()

    // Because the self.image is an optional var, you need to unwrap it
    if let image = self.image {
        self.imageView.image = self.image
    }
}

答案 1 :(得分:1)

基于上面的答案,你试图将图像设置为图像视图,当你应该尝试将图像设置为图像视图的图像时,它应该是:

func viewDidLoad() {
    super.viewDidLoad()

    // Because the self.image is an optional var, you need to unwrap it
    if let image = self.image {
        self.imageView.image = self.image
    } 
}