小编典典

Swift-UIImagePickerController-如何使用它?

swift

我正在努力理解它是如何工作的,但是对我来说却很难。=)我有1个视图,有一个按钮和一个小的ImageView区域可供预览。该按钮触发imagepickercontroller,UIView将显示拾取的图像。没有错误,但是图像未显示在UIImageView区域中。

var imagePicker = UIImagePickerController()
@IBOutlet var imagePreview : UIImageView

@IBAction func AddImageButton(sender : AnyObject) {
    imagePicker.modalPresentationStyle = UIModalPresentationStyle.CurrentContext
    imagePicker.delegate = self
    self.presentModalViewController(imagePicker, animated: true)

}
func imagePickerController(picker: UIImagePickerController!, didFinishPickingMediaWithInfo info:NSDictionary!) {
    var tempImage:UIImage = info[UIImagePickerControllerOriginalImage] as UIImage
    imagePreview.image  = tempImage

    self.dismissModalViewControllerAnimated(true)

}

func imagePickerControllerDidCancel(picker: UIImagePickerController!) {

    self.dismissModalViewControllerAnimated(true)
}

阅读 681

收藏
2020-07-07

共1个答案

小编典典

您正在抓取UIImage具名,UIImagePickerControllerOriginalImage并且不存在此类图像。你注定要抓住UIImage用钥匙UIImagePickerControllerOriginalImageeditingInfo词典:

let tempImage = editingInfo[UIImagePickerControllerOriginalImage] as! UIImage
2020-07-07