:使用的时候要判断是否拥有使用相机和相册的权限:
/// 相机权限
class func isRightCamera() -> Bool {
let authStatus = AVCaptureDevice.authorizationStatus(for: AVMediaType.video)
return authStatus != .restricted && authStatus != .denied
}
/// 相册权限
class func IsOpenAlbum() -> Bool{
let authStatus = ALAssetsLibrary.authorizationStatus()
return authStatus != .restricted && authStatus != .denied
}
一:如果我们修改头像什么的,就会用到图像的裁剪,我这边是裁剪成方形,用的系统方法
// 改变头像按钮的点击方法
@objc func changeIconBtnClick()
{
createAlertView()
}
func createAlertView() {
let alertView = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
let cameraAction = UIAlertAction(title: "相机", style: .default) {_ in
if Tools.isRightCamera() == false {
Tools.toast("未获取相机权限,请检查相机设置")
return
}
self.openCamera()
}
let albumAction = UIAlertAction(title: "相册", style: .default) { _ in
if Tools.IsOpenAlbum() == false {
Tools.toast("未获取相册权限,请检查相册设置")
return
}
self.openAlbum()
}
let cancelAction = UIAlertAction(title: "取消", style: .cancel, handler: nil)
alertView.addAction(cameraAction)
alertView.addAction(albumAction)
alertView.addAction(cancelAction)
present(alertView, animated: true, completion: nil)
}
//MARK: - UIImagePickerControllerDelegate,UINavigationControllerDelegate
extension PerfectUserInfoViewController: UIImagePickerControllerDelegate,UINavigationControllerDelegate {
func openCamera() {
let picker = UIImagePickerController()
picker.allowsEditing = true // 设置成可编辑
picker.delegate = self
if UIImagePickerController.isCameraDeviceAvailable(.rear) {
picker.sourceType = .camera
self.present(picker, animated: true, completion: nil)
} else {
Tools.toast("暂无相机权限")
}
}
func openAlbum() {
let picker = UIImagePickerController()
picker.allowsEditing = true // 设置成可编辑
picker.delegate = self
if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
picker.sourceType = .photoLibrary
self.present(picker, animated: true, completion: nil)
} else {
Tools.toast("暂无相册权限")
}
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
// 这里选择editedImage
let image = info[UIImagePickerController.InfoKey.editedImage] as! UIImage
self.iconImgView?.image = image
picker.dismiss(animated: true, completion: nil)
//上传单张图片,这里的方法值适用于我自己,你们可以换成自己的上传图片的网络请求方法
let paramsDic:NSMutableDictionary = NSMutableDictionary()
paramsDic.setValue(image, forKey: "img")
NetWork.requestObjectUpload(.uploadOneImgView(paramsDic: paramsDic), onSuccess: { (result) in
let hhh = result as! [String : Any]
self.headUrl = hhh["data"] as! String
}) { (error) in
}
}
}
二:如果是上传身份证等照片,不需要裁剪,但是竖着的照片需要选择,可以如下处理:
func buttonClick() {
createAlertView()
}
func createAlertView() {
let alertView = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
let cameraAction = UIAlertAction(title: "相机", style: .default) {_ in
if Tools.isRightCamera() == false {
Tools.toast("未获取相机权限,请检查相机设置")
return
}
self.openCamera()
}
let albumAction = UIAlertAction(title: "相册", style: .default) { _ in
if Tools.IsOpenAlbum() == false {
Tools.toast("未获取相册权限,请检查相册设置")
return
}
self.openAlbum()
}
let cancelAction = UIAlertAction(title: "取消", style: .cancel, handler: nil)
alertView.addAction(cameraAction)
alertView.addAction(albumAction)
alertView.addAction(cancelAction)
present(alertView, animated: true, completion: nil)
}
//MARK: - UIImagePickerControllerDelegate,UINavigationControllerDelegate
extension BindOSIdentityViewController: UIImagePickerControllerDelegate,UINavigationControllerDelegate {
func openCamera() {
let picker = UIImagePickerController()
picker.allowsEditing = false // 这里设置成不可编辑
picker.delegate = self
if UIImagePickerController.isCameraDeviceAvailable(.rear) {
picker.sourceType = .camera
self.present(picker, animated: true, completion: nil)
} else {
Tools.toast("暂无相机权限")
}
}
func openAlbum() {
let picker = UIImagePickerController()
picker.allowsEditing = false // 这里设置成不可编辑
picker.delegate = self
if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
picker.sourceType = .photoLibrary
self.present(picker, animated: true, completion: nil)
} else {
Tools.toast("暂无相册权限")
}
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
let img = info[UIImagePickerController.InfoKey.originalImage] as! UIImage
let image = UIImage.fixOrientation(img)()
self.upLoadIdentityCardView?.imgView1?.image = image
picker.dismiss(animated: true, completion: nil)
//上传单张图片,这里的方法值适用于我自己,你们可以换成自己的上传图片的网络请求方法
let paramsDic:NSMutableDictionary = NSMutableDictionary()
paramsDic.setValue(image, forKey: "img")
NetWork.requestObjectUpload(.uploadOneImgView(paramsDic: paramsDic), onSuccess: { (result) in
let hhh = result as! [String : Any]
self.imageUrl1 = hhh["data"] as! String
}) { (error) in
}
}
}
// 修复图片旋转
extension UIImage {
func fixOrientation() -> UIImage {
if self.imageOrientation == .up, self.imageOrientation == .upMirrored {
return self
}
// let flipImageOrientation = (self.imageOrientation.rawValue + 2) % 8
var count = 0
if self.imageOrientation.rawValue == 1 {
count = 1
}
let flipImage = UIImage(cgImage:self.cgImage!,
scale:self.scale,
orientation:UIImage.Orientation(rawValue: count)!)
return flipImage
}
}
网友评论