1、控制器继承
class FGVerifiedVC:FGBaseViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate
2、底部弹出Alert方法
/// 屏幕底部弹出的Alert
funcshowBottomAlert(type:Bool){
//设置选择相片类型(正面或反面)
isChoose=type
letalertController=UIAlertController(title:”选择图片”, message:”选择拍照或本地相册”, preferredStyle: .actionSheet)
letcancel=UIAlertAction(title:”取消”, style: .cancel, handler: {
actionin
}) lettakingPictures=UIAlertAction(title:”拍照”, style: .default) { actionin self.goCamera() } letlocalPhoto=UIAlertAction(title:”本地图片”, style: .default) { actionin self.goImage() } alertController.addAction(cancel) alertController.addAction(takingPictures) alertController.addAction(localPhoto) self.present(alertController, animated:true, completion:nil)}
3、拍照与本地相册方法
/// 去拍照
funcgoCamera(){
//先设定sourceType为相机,然后判断相机是否可用(ipod)没相机,不可用将sourceType设定为相片库
var sourceType = UIImagePickerControllerSourceType.camera
if !UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera){
sourceType =UIImagePickerControllerSourceType.photoLibrary
}
let picker = UIImagePickerController()
picker.delegate=self
picker.allowsEditing=true//设置可编辑
picker.sourceType= sourceType
self.present(picker, animated:true, completion:nil)//进入照相界面
}
/// 去相册
funcgoImage(){
letpickerImage =UIImagePickerController()
if !UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.photoLibrary){
pickerImage.sourceType = UIImagePickerControllerSourceType.photoLibrary
pickerImage.mediaTypes=UIImagePickerController.availableMediaTypes(for: pickerImage.sourceType)!
}
pickerImage.delegate=self
pickerImage.allowsEditing=true
self.present(pickerImage, animated:true, completion:nil)
}
//选择好照片后choose后执行的方法
funcimagePickerController(_picker:UIImagePickerController, didFinishPickingMediaWithInfo info: [String:Any]) {
print("choose--------->>")
print(info)
ifisChoose!{
imgPositivePic = info[UIImagePickerControllerEditedImage] as! UIImage
//imgTest.image = img
btnAddPositivePic.setBackgroundImage(imgPositivePic, for: .normal)
}else{
imgBlackPic = info[UIImagePickerControllerEditedImage] as! UIImage
//imgTest.image = img
btnAddBlackPic.setBackgroundImage(imgBlackPic, for: .normal)
}
picker.dismiss(animated:true, completion:nil)
}
//cancel后执行的方法
funcimagePickerControllerDidCancel(_picker:UIImagePickerController) {
print("cancel--------->>")
picker.dismiss(animated:true, completion:nil)
}
网友评论