当前位置: 首页 > 面试题库 >

如何同时快速为相机和照片库制作UIImagePickerController

谭飞掣
2023-03-14
问题内容

我使用UIImagePickerController通过iPhone的相机拍摄照片。

我想同时显示“拍照”和“选择照片”。

我的密码

imagePicker =  UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .camera
//imagePicker.sourceType = .PhotoLibrary
presentViewController(imagePicker, animated: true, completion: nil)

我试图使用imagePicker.sourceType = .CameraimagePicker.sourceType = .PhotoLibrary一起做这一点,但它不工作…

谢谢


问题答案:

导入UIImagePickerControllerDelegate并创建一个变量来分配UIImagePickerController var imagePicker = UIImagePickerController()和set imagePicker.delegate = self

创建一个操作表以显示“相机”和“照片库”的选项。

在按钮上单击操作:

@IBAction func buttonOnClick(_ sender: UIButton)
{
    self.btnEdit.setTitleColor(UIColor.white, for: .normal)
    self.btnEdit.isUserInteractionEnabled = true

    let alert = UIAlertController(title: "Choose Image", message: nil, preferredStyle: .actionSheet)
    alert.addAction(UIAlertAction(title: "Camera", style: .default, handler: { _ in
        self.openCamera()
    }))

    alert.addAction(UIAlertAction(title: "Gallery", style: .default, handler: { _ in
        self.openGallary()
    }))

    alert.addAction(UIAlertAction.init(title: "Cancel", style: .cancel, handler: nil))

    /*If you want work actionsheet on ipad
    then you have to use popoverPresentationController to present the actionsheet,
    otherwise app will crash on iPad */
    switch UIDevice.current.userInterfaceIdiom {
    case .pad:
        alert.popoverPresentationController?.sourceView = sender
        alert.popoverPresentationController?.sourceRect = sender.bounds
        alert.popoverPresentationController?.permittedArrowDirections = .up
    default:
        break
    }

    self.present(alert, animated: true, completion: nil)
}

func openCamera()
{
    if(UIImagePickerController .isSourceTypeAvailable(UIImagePickerControllerSourceType.camera))
    {
        imagePicker.sourceType = UIImagePickerControllerSourceType.camera
        imagePicker.allowsEditing = true
        self.present(imagePicker, animated: true, completion: nil)
    }
    else
    {
        let alert  = UIAlertController(title: "Warning", message: "You don't have camera", preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
        self.present(alert, animated: true, completion: nil)
    }
}

func openGallary()
{
    imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary
    imagePicker.allowsEditing = true
    self.present(imagePicker, animated: true, completion: nil)
}

从此处下载示例项目。



 类似资料:
  • 我的应用程序提供了一个摄像头,允许用户拍照或录制视频。我不可能知道用户将选择哪个操作。所以我需要为两个动作提供相机预览。使用旧的camera API没有任何问题,但我不确定camera2 API。

  • 我正在尝试允许用户用相机应用程序拍摄一张照片,然后在应用程序中显示它的预览。 我还需要在应用程序的稍后阶段访问该文件(但这不是一个问题) 我有以下崩溃: 完成时处理图像: 我不明白为什么它只是4.4.4的问题,而不是其他操作系统。 当出现崩溃时,它并没有说我的应用程序崩溃了,它说Camera已经停止工作了,并且我只能在没有过滤器的情况下查看日志时查看崩溃,这意味着崩溃不在我的应用程序中?

  • 我是android studio和手机应用程序开发的初学者,但我仍在努力学习。因此,我设法构建了我的第一个webview应用程序来显示页面和图片。我发现了许多有用的资源来帮助我这样做,但我仍然面临着一个问题,即如何直接上传从手机摄像头(而不是从图库)拍摄的照片并上传到服务器。 1-在我按下眉毛按钮后,应用程序会提示我是从相机还是从画廊拍摄照片。(Android版9)2-当我从图库上传一张照片时,应

  • 学习如何用Chrome DevTools 堆分析器录制堆快照并查找内存泄漏。 Chrome DevTools 堆分析器显示页面 JavaScript 对象和相关 DOM 节点的内存分配 (请参见Objects retaining tree)。使用它来获取 JS 堆快照、 分析内存图、 比较快照,查找内存泄漏。 生成快照 在Profiles(分析)面板中,选择 Take Heap Snapshot(

  • 问题内容: 我在通过Android按钮单击时无法找到打开用户相机胶卷/照片库的方法。我正在寻找的解决方案包括访问相机胶卷/照片厨房,允许用户选择图像,将所选图像保存到文件中,然后最后将该文件保存到诸如parse.com之类的数据库中。请帮助并提前感谢您! 请参考下面的链接。我希望实现类似于IOS上的实现。 https://drive.google.com/file/d/0B4jjwQsKhqZRc

  • 对于伸缩/故障转移,mongodb使用“副本集”,其中有一个主服务器和一个或多个辅助服务器。Primary用于写操作。中学用于阅读。这几乎是SQL编程中使用的主从模式。如果主节点下降,则次节点集群中的次节点将取代它的位置。因此,水平伸缩和故障转移的问题得到了解决。然而,这似乎不是一个允许分叉的解决方案。一个真正的碎片只保存整个数据的一部分,所以如果副本集中的次要碎片是碎片,当它没有服务请求所需的所