ios - 如何快速从 UIAlertAction 打开照片库?

标签 ios iphone xcode swift

我正在制作一个应用程序,用户可以将他的图像添加到表格 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/

相关文章:

ios - 应该显式调用 willMoveToParentViewController :(UIViewController *)parent & didMoveToParentViewController:(UIViewController *)parent?

iphone - ios 7 模拟器不会为针对 ios 6.1 sdk 编译的应用程序回退用户界面

objective-c - "Cinch App"是怎么做到的呢?

ios - UICollectionView 重新加载数据导致图形故障

iphone - 如何在越狱的 iOS 设备上访问沙箱之外的文件系统?

ios - 如果在 iPhone 的蓝牙设置中建立连接,我如何收到通知?

ios - 动画 self.view.frame?

iphone - UIAlertView 显示两个相同颜色的按钮

ios - App Thinning - 如果开发人员未提供所有 Assets ,它是如何工作的

ios - Swift 中 ColorMatrix 的亮度、饱和度和对比度