如何在swift中从UIAlertAction打开照片库?

时间:2015-02-21 01:22:10

标签: ios iphone xcode swift

我正在创建一个应用程序,用户可以将其图像添加到表格视图列表中。当他到达视野时,会出现一个警报,动作表格,弹出询问他是否要拍照,从图书馆中选择或取消。

我遇到第二个问题。我可以使用以下代码打开带有按钮的库:

@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一起使用时,它不起作用。有谁可以帮助我吗?

由于

1 个答案:

答案 0 :(得分:2)

class ViewController: UIViewController, UIImagePickerControllerDelegate

然后在您想要打开code的地方写下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

}