UIView setImage:]:发送到实例的无法识别的选择器

时间:2015-09-18 03:11:36

标签: ios swift uiimageview

import UIKit
import MobileCoreServices

class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate{
    let imagePicker = UIImagePickerController()


    @IBOutlet var ImageView: UIImageView!
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        imagePicker.delegate = self
    }



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


    // MARK: Actions


    @IBAction func Select(sender: AnyObject) {

        //imagePicker.sourceType =
          //  UIImagePickerControllerSourceType.PhotoLibrary
        //imagePicker.mediaTypes = [kUTTypeImage as String]
        imagePicker.allowsEditing = false
        self.presentViewController(imagePicker, animated: true, completion: nil)

    }

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


        if  let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
            ImageView.contentMode = .ScaleAspectFit
            ImageView.image = image
        }

        self.dismissViewControllerAnimated(true, completion: nil)

    }

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

我难以理解为什么PickerController选择的UIImage没有设置为UIImageView。任何人都可以提供我在这里缺少的信息吗?

2 个答案:

答案 0 :(得分:7)

您的代码中有@IBOutlet var ImageView: UIImageView!,但是您收到错误" UIView setImage:]:无法识别的选择器"。这意味着您在Interface Builder中将视图配置为UIView,而不是UIImageView。您需要设置UIImageView才能设置图像。

答案 1 :(得分:0)

仅供将来参考,可能发生这种情况的另一个原因是因为您意外地将错误的UI元素与您的UIImageView属性相关联。

例如,我将NSConstraint拖到了UIImageView,并得到了相同的错误。