简体   繁体   English

如何快速从UIAlertAction打开图片库?

[英]How to open photo library from an UIAlertAction in swift?

I'm making an app that the user can add his image to a table view list. 我正在开发一个应用程序,用户可以将其图像添加到表视图列表中。 When he gets to the view, an alert, action sheet styled, pops up asking him if he wants to take the picture, choose from the library or cancel. 当他进入视图时,弹出一个警报,样式为动作表,询问他是否要拍照,从图库中选择还是取消。

I'm having trouble with the second. 我遇到第二个问题。 I'm able to open the library with buttons using the following code: 我可以使用以下代码通过按钮打开库:

@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

        }

But when I try using this code with an UIAlertAction it doesn't work. 但是,当我尝试将此代码与UIAlertAction结合使用时,它将无法正常工作。 Can someone help me, please? 有人能帮助我吗?

Thanks 谢谢

class ViewController: UIViewController, UIImagePickerControllerDelegate

Then write following code where you want to open UIImagePickerController . 然后在要打开UIImagePickerController位置编写以下code

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)
        }

Then implement following delegate method. 然后实现以下delegate方法。

func imagePickerController(picker: UIImagePickerController!, didFinishPickingImage image: UIImage!, editingInfo: NSDictionary!){
    self.dismissViewControllerAnimated(true, completion: { () -> Void in

    })

    imageView.image = image

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM