选择图像后,快速图像选择器未关闭

时间:2014-07-28 18:39:54

标签: swift

我可以打开图像库并选择图像但是当我选择图像时,取消部分的选择不会关闭。我错过了什么吗?

func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {

    var imagePickerController = UIImagePickerController()
    imagePickerController.delegate = self
    imagePickerController.sourceType = UIImagePickerControllerSourceType.SavedPhotosAlbum
    imagePickerController.allowsEditing = true
    self.presentViewController(imagePickerController, animated: true, completion: { imageP in

        })
}


func imagePickerController(picker: UIImagePickerController!, didFinishPickingImage image: UIImage!, editingInfo: NSDictionary!) {
    let selectedImage : UIImage = image
    println(selectedImage)
}

2 个答案:

答案 0 :(得分:5)

您需要在

中关闭视图控制器
func imagePickerController(picker: UIImagePickerController!, didFinishPickingImage image: UIImage!, editingInfo: NSDictionary!) {
let selectedImage : UIImage = image
println(selectedImage)
self.dismissViewControllerAnimated(true, completion: nil)

}

答案 1 :(得分:1)

您未正确使用presentViewController上的完成功能块。

@IBAction func share(sender: UIBarButtonItem) {
  let imagePicker = UIImagePickerController()
  imagePicker.delegate = self
  imagePicker.sourceType = .SavedPhotosAlbum
  presentViewController(imagePicker, animated: true, completion: nil)
}

然后使用委托调用来关闭选择器。

func imagePickerController(picker: UIImagePickerController!, didFinishPickingMediaWithInfo info: NSDictionary!) {
  let selectedImage = info[UIImagePickerControllerOriginalImage] as UIImage
  dismissViewControllerAnimated(true, completion: nil)
}