簡體   English   中英

無法將從相機拍攝的圖像傳遞到另一個ViewController

[英]Unable to pass image taken from camera to another viewcontroller

我有一個按鈕可以拍照。 我希望將相機拍攝的圖像傳遞給另一個視圖控制器。第一個視圖控制器的代碼如下所示。

    @IBAction func takePhoto(sender: AnyObject)
{   let picker = UIImagePickerController()
    picker.delegate = self
    picker.sourceType = .Camera
    presentViewController(picker, animated: true,completion : nil)

}


func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject])
{
    TakenImage = info[UIImagePickerControllerOriginalImage] as? UIImage ; dismissViewControllerAnimated(true, completion: nil )

let controller = self.storyboard!.instantiateViewControllerWithIdentifier("NoteDetailViewController") as! NoteDetailViewController

    controller.takinPhoto = true

    if (note != "")
    {
        controller.content = note
    }

    controller.imageFromCamera = TakenImage

    if (self.tags != "")
    {
        controller.tagsTextField.text = self.tags
    }
     self.presentViewController(controller, animated: true, completion: nil)
}
@IBAction func save(sender: AnyObject)
{
    UIImageWriteToSavedPhotosAlbum(TakenImage!, self, "image:didFinishSavingWithError:contextInfo:", nil)
}
func image(image: UIImage, didFinishSavingWithError error: NSError?, contextInfo:UnsafePointer<Void>)
{
    if error == nil {
        let ac = UIAlertController(title: "Saved!", message: "Your altered image has been saved to your photos.", preferredStyle: .Alert)
        ac.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
        presentViewController(ac, animated: true, completion: nil)
    } else
    {
        let ac = UIAlertController(title: "Save error", message: error?.localizedDescription, preferredStyle: .Alert)
        ac.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
        presentViewController(ac, animated: true, completion: nil)
    }
}

我的第二個viewcontroller的代碼如下所示

if (takinPhoto == true)
    {
        if (imageFromCamera != nil)
        {
            if let image1 = self.imageFromCamera
            {
                self.imageView2.image = image1
            }

        }
        if (self.content != "")
        {
            self.contentTextField2.text = content
        }

    }

但是相機的圖像沒有出現在第二個ViewController中。我該如何解決?

更新的答案。

我重寫了您的代碼,並使其能夠在本地工作。 主要更改是使用了特定於照片的其他委托方法。

@IBAction func takePhoto(sender: AnyObject) {   let picker = UIImagePickerController()
    picker.delegate = self
    picker.sourceType = .Camera
    presentViewController(picker, animated: true,completion : nil)
}

func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {
    takenImage = image
    dismissViewControllerAnimated(true, completion: nil)
}

@IBAction func save(sender: AnyObject) {
    UIImageWriteToSavedPhotosAlbum(takenImage!, self, #selector(ViewController.image(_:didFinishSavingWithError:contextInfo:)), nil)
}

func image(image: UIImage, didFinishSavingWithError error: NSError?, contextInfo:UnsafePointer<Void>) {
    if error == nil {
        let ac = UIAlertController(title: "Saved!", message: "Your altered image has been saved to your photos.", preferredStyle: .Alert)
        ac.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
        presentViewController(ac, animated: true, completion: nil)
    } else {
        let ac = UIAlertController(title: "Save error", message: error?.localizedDescription, preferredStyle: .Alert)
        ac.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
        presentViewController(ac, animated: true, completion: nil)
    }
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if (segue.identifier == "saveTakenImage") {
        let controller = segue.destinationViewController as! NoteDetailViewController
        controller.takinPhoto = true
        if (note != "") {
            controller.content = note
        }

        controller.imageFromCamera = takenImage

        if (self.tags != "") {
            controller.tagsTextField.text = self.tags
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM