用系统的UIAlertAction弹出相册和拍照按钮
let sheet = UIAlertController(title: "Media messages", message: nil, preferredStyle: .actionSheet)
//从相册选择
let photoAction = UIAlertAction(title: "Send photo", style: .default) { (action) in
if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
let picker = UIImagePickerController()
picker.sourceType = .photoLibrary
picker.delegate = self
self.present(picker, animated: true, completion: {
})
} else {
print("模拟其中无法打开相册,请在真机中使用")
}
}
//拍照
let takePhotoAction = UIAlertAction(title: "Take photo", style: .default) { (action) in
if UIImagePickerController.isSourceTypeAvailable(.camera) {
let picker = UIImagePickerController()
picker.sourceType = .camera
picker.delegate = self
self.present(picker, animated: true, completion: {
})
} else {
print("模拟其中无法打开照相机,请在真机中使用")
}
}
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
sheet.addAction(photoAction)
sheet.addAction(takePhotoAction)
sheet.addAction(cancelAction)
self.present(sheet, animated: true, completion: nil)
注意:如果是ios10以上,需要在plist.info中单独设置权限