- 点击按钮的方法
func photos() { self.showBottomAlert() }
屏幕底部弹出的Alert
func showBottomAlert(){
let alertController=UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
let cancel=UIAlertAction(title:"取消", style: .cancel, handler: nil)
let takingPictures=UIAlertAction(title:"拍照", style: .default)
{
action in
self.goCamera()
}
let localPhoto=UIAlertAction(title:"本地图片", style: .default)
{
action in
self.goImage()
}
alertController.addAction(cancel)
alertController.addAction(takingPictures)
alertController.addAction(localPhoto)
self.present(alertController, animated:true, completion:nil)
}
拍照与本地相册方法
- 去拍照
func goCamera(){
if UIImagePickerController.isSourceTypeAvailable(.camera){
let cameraPicker = UIImagePickerController()
cameraPicker.delegate = self
cameraPicker.allowsEditing = true
cameraPicker.sourceType = .camera
//在需要的地方present出来
self.present(cameraPicker, animated: true, completion: nil)
} else {
print("不支持拍照")
}
}
- 去相册
func goImage(){
let photoPicker = UIImagePickerController()
photoPicker.delegate = self
photoPicker.allowsEditing = true
photoPicker.sourceType = .photoLibrary
//在需要的地方present出来
self.present(photoPicker, animated: true, completion: nil)
}
-
代理
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)
}
- 第三方库实现 TZImagePickerController
必须实现TZImagePickerControllerDelegate
let imagePickerVc = TZImagePickerController(maxImagesCount: 1, delegate: self)
imagePickerVc?.modalPresentationStyle = .custom
self.present(imagePickerVc!, animated: true)
// 图片选择器的代理
// TZImagePickerControllerDelegate
func imagePickerController(_ picker: TZImagePickerController!, didFinishPickingPhotos photos: [UIImage]!, sourceAssets assets: [Any]!, isSelectOriginalPhoto: Bool) {
if let image = photos.first {
//显示设置的照片
imageview.image = image
}
}