我正在使用MapView处理应用程序,我有一个显示在MapView顶部的自定义按钮。当用户点击该按钮时,我想启动相机,然后用户拍照并选择"使用照片"。除了下一部分外,我完成了所有这些工作..一旦用户选择"使用Photo",我希望捕获的图像传递给另一个功能。然后视图被释放并返回到地图..我在想这可以使用完成处理程序完成吗?但我不清楚这是否是最好的方法,如果是这样,如何将UIImage类型的图像传递给要处理的函数。有人可以帮忙吗?
我现在有以下内容:
@IBAction func mapButton(sender: AnyObject) {
let image = UIImagePickerController()
//image.delegate = self
image.sourceType = UIImagePickerControllerSourceType.Camera
image.allowsEditing = false
self.presentViewController(image, animated: true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {
self.dismissViewControllerAnimated(true, completion: customFunction(image))
}
func customFunction(image: UIImage) {
print("Do Something with Image")
}
答案 0 :(得分:4)
通过取消注释行//image.delegate = self
来设置图像选择器委托(为清楚起见,您可能希望将选择器重命名为imagePicker或imagePickerController以使其清楚这是一个选择器而不是拾取的图像。)
也弃用didFinishPickingImage
,因此最好使用didFinishPickingMediaWithInfo
在完成处理程序上调用该函数:
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage{
dismissViewControllerAnimated(true, completion: { self.customFunction(pickedImage)} )
}else{
dismissViewControllerAnimated(true, completion: nil)
}
}
或者你可以调用该函数,然后解雇选择器:
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage{
customFunction(pickedImage)
}
dismissViewControllerAnimated(true, completion: nil)
}
答案 1 :(得分:0)
委托中返回的图像可以先保存在引用中。然后关闭视图控制器,然后调用自定义方法。这将确保在解除视图控制器之前保存图像参考。
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {
let selectedImage=image;
self.dismissViewControllerAnimated(true, completion: nil)
customFunction(image);
}