我使用UIImagePickerController通过iPhone的摄像头拍照.

我想展示"拍照"和" Select 照片".

我的密码

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

我试着用imagePicker.sourceType = .CameraimagePicker.sourceType = .PhotoLibrary来做这件事,但没用...

非常感谢.

推荐答案

导入UIImagePickerControllerDelegate并创建一个变量以分配UIImagePickerController

创建一个操作表,以显示"摄像头"和"照片库"的选项.

在按钮上单击操作:

@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(UIImagePickerController.SourceType.camera))
        {
            imagePicker.sourceType = UIImagePickerController.SourceType.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 = UIImagePickerController.SourceType.photoLibrary
        imagePicker.allowsEditing = true
        self.present(imagePicker, animated: true, completion: nil)
    }

下载SwiftSwiftUI的示例项目

Swift相关问答推荐

Form中的Divider在macOS上并不完全扩展

变量捕获:变量在函数闭包中的行为

如何让CTFontCreateUIFontForLanguage与汉字和表情包协同工作?

SWIFT异步/等待,多个监听程序

如何强制创建新的UIViewControllerRenatable|更新Make UIViewController上的视图

对实例方法appendInterpolation的调用没有完全匹配

防止带有背景的文本扩展到 maxWidth

需要将 json 映射到 Swift 中的模型

在 struct 中的序列和非序列泛型函数之间进行 Select

为什么 SwiftUI 不在工具栏菜单中反映 @State 属性值?

SwiftUI:当先前的视图列表更改时,NavigationView 详细视图会弹出回栈

我如何从 UIAlertController 导航到新屏幕(swiftUI)

使用 swift 运行 pod install 时出错

在 Swift 中计算两个日期之间的差异

我可以在 Swift 中为泛型类型 T 分配默认类型吗?

为什么'nil' 与 Swift 3 中的 'UnsafePointer' 不兼容?

DispatchQueue:不能在非主线程上使用 asCopy = NO 调用

快速覆盖属性

Swift中字节数组的NSData

更改在变量的 willSet 块中设置的值