• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ios - 要求用户使用 swift2 从照片库或相机中选择照片

[复制链接]
菜鸟教程小白 发表于 2022-12-12 19:31:13 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我希望用户从他的照片库或相机中选择一张照片。我找不到任何例子。我想用 UIAlertView 之类的东西提示用户。

我的代码适用于照片库。

@IBAction func selectLeftPhoto(sender: AnyObject) {

        flag = 1

        let myPickerController = UIImagePickerController()
        myPickerController.delegate = self;
        myPickerController.sourceType = UIImagePickerControllerSourceType.PhotoLibrary

        self.presentViewController(myPickerController, animated: true, completion: nil)

    }

    @IBAction func selectRightButton(sender: AnyObject) {

        flag = 2

        let myPickerController = UIImagePickerController()
        myPickerController.delegate = self;
        myPickerController.sourceType = UIImagePickerControllerSourceType.PhotoLibrary

        self.presentViewController(myPickerController, animated: true, completion: nil)

    }



    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject])
    {

        let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage

        if flag == 1 {
            leftImage.image = pickedImage
            let imageData = UIImageJPEGRepresentation(pickedImage!, 0.5)
            let base64String = imageData!.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0))
            photo1 = base64String

        }else if flag == 2 {
            rightImage.image = pickedImage
            let imageData = UIImageJPEGRepresentation(pickedImage!, 0.5)
            let base64String = imageData!.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0))
            photo2 = base64String
        }

        dismissViewControllerAnimated(true, completion: nil)



    }

我的用户界面:

enter image description here

我想在点击选择照片按钮后提示用户从照片库或相机中进行选择。



Best Answer-推荐答案


        let alert:UIAlertController=UIAlertController(title: "Choose Image", message: nil, preferredStyle: UIAlertControllerStyle.ActionSheet)
        let cameraAction = UIAlertAction(title: "Camera", style: UIAlertActionStyle.Default)
        {
            UIAlertAction in
            self.openCamera()

        }
        let gallaryAction = UIAlertAction(title: "Gallary", style: UIAlertActionStyle.Default)
        {
            UIAlertAction in
            self.openGallary()
        }
        let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel)
        {
            UIAlertAction in

        }
        // Add the actions
        picker?.delegate = self
        alert.addAction(cameraAction)
        alert.addAction(gallaryAction)
        alert.addAction(cancelAction)
        // Present the controller
        if UIDevice.currentDevice().userInterfaceIdiom == .Phone
        {
            self.presentViewController(alert, animated: true, completion: nil)
        }
        else
        {
            popover=UIPopoverController(contentViewController: alert)
            popover!.presentPopoverFromRect(btnClickMe.frame, inView: self.view, permittedArrowDirections: UIPopoverArrowDirection.Any, animated: true)
        }

关于ios - 要求用户使用 swift2 从照片库或相机中选择照片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36835286/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap