【问题标题】:How to open photo library from an UIAlertAction in swift?如何快速从 UIAlertAction 打开照片库?
【发布时间】:2015-02-21 01:22:09
【问题描述】:

我正在制作一个应用程序,用户可以将他的图像添加到表格视图列表中。当他到达视图时,会弹出一个警报、操作表样式,询问他是否要拍照、从库中选择或取消。

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

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

谢谢

【问题讨论】:

    标签: ios iphone xcode swift


    【解决方案1】:
    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
    
    }
    

    【讨论】:

    • 我只是没有得到第二部分,我的意思是,如何获取用户选择的图像并将其放入 UIImageView 中?
    • image 进入method argument 的对象是你的selected image
    • 别忘了将UINavigationControllerDelegate 添加到您的班级。
    猜你喜欢
    • 2016-12-06
    • 2015-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-25
    • 1970-01-01
    相关资源
    最近更新 更多