//点击按钮的方法
1 func photos() { 2 3 self.showBottomAlert() 4 5 }
/// 屏幕底部弹出的Alert
1 func showBottomAlert(){ 2 3 let alertController=UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet) 4 5 let cancel=UIAlertAction(title:"取消", style: .cancel, handler: nil) 6 let takingPictures=UIAlertAction(title:"拍照", style: .default) 7 { 8 action in 9 self.goCamera() 10 11 } 12 let localPhoto=UIAlertAction(title:"本地图片", style: .default) 13 { 14 action in 15 self.goImage() 16 17 } 18 alertController.addAction(cancel) 19 alertController.addAction(takingPictures) 20 alertController.addAction(localPhoto) 21 self.present(alertController, animated:true, completion:nil) 22 23 }
//拍照与本地相册方法/
// 去拍照
1 func goCamera(){ 2 3 if UIImagePickerController.isSourceTypeAvailable(.camera){ 4 let cameraPicker = UIImagePickerController() 5 cameraPicker.delegate = self 6 cameraPicker.allowsEditing = true 7 cameraPicker.sourceType = .camera 8 //在需要的地方present出来 9 self.present(cameraPicker, animated: true, completion: nil) 10 } else { 11 12 print("不支持拍照") 13 14 } 15 16 }
/// 去相册
1 func goImage(){ 2 3 4 let photoPicker = UIImagePickerController() 5 photoPicker.delegate = self 6 photoPicker.allowsEditing = true 7 photoPicker.sourceType = .photoLibrary 8 //在需要的地方present出来 9 self.present(photoPicker, animated: true, completion: nil) 10 11 }
//代理
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediawithInfo info: [UIImagePickerController.InfoKey : Any]) { print("获得照片============= \(info)") let image : UIImage = info[UIImagePickerController.InfoKey.editedImage] as! UIImage //显示设置的照片 imgView.image = image self.dismiss(animated: true, completion: nil) }
作者:江河_ios
链接:https://www.jianshu.com/p/fc0768999ef8
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。