“致命错误:在展开可选值时意外发现nil”用于图像视图

时间:2015-12-29 20:17:06

标签: swift uiimagepickercontroller

我有一个视图,有一个按钮和一个ImageView。按下时,按钮触发UIImagePickerController,UIView将显示拾取的图像。我尝试在不使用情节提要的情况下执行此操作,但我一直在此"fatal error: unexpectedly found nil while unwrapping an Optional value"处获得imageView.contentMode = .ScaleAspectFit。我查看了其他建议使用editingInfo[UIImagePickerControllerOriginalImage]而不是info[UIImagePickerControllerOriginalImage]的stackoverflow帖子,但它没有用。你是如何解决这个问题的?

import UIKit

class EditViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

var imageView: UIImageView!=nil

override func viewDidLoad() {
    super.viewDidLoad()
    createBackground()
    createImageView()
    createButton()
}

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

func createBackground() {
    let backgroundImage = UIImage(named: "pinetree")
    var imageView: UIImageView!
    imageView = UIImageView(frame: view.bounds)
    imageView.contentMode = .ScaleAspectFill
    imageView.clipsToBounds = true
    imageView.image = backgroundImage
    imageView.center = view.center
    view.addSubview(imageView)
    self.view.sendSubviewToBack(imageView)
}

func createImageView() {
    var image: UIImage!
    var imageView = UIImageView(image: image)
    imageView.frame = CGRectMake(view.bounds.width/2, view.bounds.height/2, view.bounds.width, view.bounds.height*(3/4))
    self.view.addSubview(imageView)
}

func createButton() {
    let size = view.bounds.width
    let cx = view.bounds.width/2
    let cy = view.bounds.height * 4/5
    let button = UIButton();
    button.frame = CGRectMake(0, 0, size, size)
    button.center.x = cx
    button.center.y = cy
    button.setTitle("Load Image", forState: .Normal)
    button.addTarget(self, action: "loadImageButtonPressed:", forControlEvents: .TouchUpInside)
    self.view.addSubview(button)
}

func loadImageButtonPressed(sender: UIButton!) {
    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.PhotoLibrary){
        var imagePicker = UIImagePickerController()
        imagePicker.delegate = self
        imagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary;
        //imagePicker.mediaTypes = [kUTTypeImage];
        imagePicker.allowsEditing = false
        self.presentViewController(imagePicker, animated: true, completion: nil)
    }
}

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
    if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
        imageView.contentMode = .ScaleAspectFit
        imageView.image = pickedImage
    }
    dismissViewControllerAnimated(true, completion: nil)
}

func imagePickerControllerDidCancel(picker: UIImagePickerController) {
    dismissViewControllerAnimated(true, completion: nil)
}

}

1 个答案:

答案 0 :(得分:1)

我找到了答案!我意识到我已经在函数imageView中的类EditViewController&中启动了变量createImageView()两次。我删除了函数imageView中的createImageView()并编辑了类中imageView的启动。我的更新代码是:

import UIKit

class EditViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate {

var imageView = UIImageView(image: UIImage(named: "ClearImage"))

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

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)
    createBackground()
    createImageView()
    createButton()
}

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

func createBackground() {
    let backgroundImage = UIImage(named: "pinetree")
    var imageView: UIImageView!
    imageView = UIImageView(frame: view.bounds)
    imageView.contentMode = .ScaleAspectFill
    imageView.clipsToBounds = true
    imageView.image = backgroundImage
    imageView.center = view.center
    view.addSubview(imageView)
    self.view.sendSubviewToBack(imageView)
}

func createImageView() {
    imageView.frame = CGRectMake(0,0, view.bounds.width, view.bounds.height*(3/4))
    self.view.addSubview(imageView)
}

func createButton() {
    let size = view.bounds.width
    let cx = view.bounds.width/2
    let cy = view.bounds.height * 4/5
    let button = UIButton();
    button.frame = CGRectMake(0, 0, 200, 200)
    button.center.x = cx
    button.center.y = cy
    button.setTitle("Load Image", forState: .Normal)
    button.addTarget(self, action: "loadImageButtonPressed:", forControlEvents: .TouchUpInside)
    self.view.addSubview(button)
}

func loadImageButtonPressed(sender: UIButton!) {
    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.PhotoLibrary){
        var imagePicker = UIImagePickerController()
        imagePicker.delegate = self
        imagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary;
        //imagePicker.mediaTypes = [kUTTypeImage];
        imagePicker.allowsEditing = false
        self.presentViewController(imagePicker, animated: true, completion: nil)
    }
}

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
    if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
        imageView.contentMode = .ScaleAspectFit
        imageView.image = pickedImage
    }
    dismissViewControllerAnimated(true, completion: nil)
}


func imagePickerControllerDidCancel(picker: UIImagePickerController) {
    dismissViewControllerAnimated(true, completion: nil)
}

}