我正在制作一个应用程序,用户可以将他的图像添加到表格 View 列表中。当他到达 View 时,会弹出一个警报、操作表样式,询问他是要拍照、从图库中选择还是取消。
第二个我遇到了麻烦。我可以使用以下代码通过按钮打开库:
@IBOutlet weak var imageView: UIImageView!
@IBAction func loadPhoto(sender: AnyObject) {
let pickerC = UIImagePickerController()
pickerC.delegate = self
self.presentViewController(pickerC, animated: true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController!, didFinishPickingMediaWithInfo info: NSDictionary!) {
self.dismissViewControllerAnimated(true, completion: nil);
let gotImage = info[UIImagePickerControllerOriginalImage] as UIImage
imageView.image = gotImage
}
但是当我尝试将此代码与 UIAlertAction 一起使用时,它不起作用。有人能帮助我吗?
谢谢
最佳答案
class ViewController: UIViewController, UIImagePickerControllerDelegate
然后在要打开UIImagePickerController
的地方写下代码
。
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.PhotoLibrary
){
println("Button capture")
var imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary;
imagePicker.allowsEditing = false
self.presentViewController(imagePicker, animated: true, completion: nil)
}
然后实现下面的delegate
方法。
func imagePickerController(picker: UIImagePickerController!, didFinishPickingImage image: UIImage!, editingInfo: NSDictionary!){
self.dismissViewControllerAnimated(true, completion: { () -> Void in
})
imageView.image = image
}
关于ios - 如何快速从 UIAlertAction 打开照片库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28640946/