swift之系统方法获取相册权限、打开相机(拍照、录像)、相册
2018-03-01 16:06
253 查看
引入import AVFoundation
import Photos添加代理:UIImagePickerControllerDelegate,UINavigationControllerDelegate
func openCamera(){//打开相册 let pickImageController:UIImagePickerController=UIImagePickerController.init() if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.photoLibrary){ //获取相册权限 PHPhotoLibrary.requestAuthorization({ (status) in switch status { case .notDetermined: break case .restricted://此应用程序没有被授权访问的照片数据 break case .denied://用户已经明确否认了这一照片数据的应用程序访问 break case .authorized://已经有权限 //跳转到相机或者相册 pickImageController.delegate=self as UIImagePickerControllerDelegate & UINavigationControllerDelegate pickImageController.allowsEditing = true pickImageController.sourceType = UIImagePickerControllerSourceType.photoLibrary; //弹出相册页面或相机 self.present(pickImageController, animated: true, completion: { }) break } }) } //打开相机拍照 if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera){ AVCaptureDevice.requestAccess(for: AVMediaType.video, completionHandler: { (ist) in let status:AVAuthorizationStatus=AVCaptureDevice.authorizationStatus(for: AVMediaType.video) if status==AVAuthorizationStatus.authorized {//获得权限 //跳转到相机或者相册 pickImageController.delegate=self as UIImagePickerControllerDelegate & UINavigationControllerDelegate pickImageController.allowsEditing = true pickImageController.sourceType = UIImagePickerControllerSourceType.camera; //弹出相册页面或相机 self.present(pickImageController, animated: true, completion: { }) }else if (status==AVAuthorizationStatus.denied)||(status==AVAuthorizationStatus.restricted) { } }) } } //去设置权限 func gotoSetting(){ let alertController:UIAlertController=UIAlertController.init(title: "去设置", message: "设置-》通用-》", preferredStyle: UIAlertControllerStyle.alert) let sure:UIAlertAction=UIAlertAction.init(title: "去开启权限", style: UIAlertActionStyle.default) { (ac) in let url=URL.init(string: UIApplicationOpenSettingsURLString) if UIApplication.shared.canOpenURL(url!){ UIApplication.shared.open(url!, options: [:], completionHandler: { (ist) in }) } } alertController.addAction(sure) self.present(alertController, animated: true) { } }要将MobileCoreServices 框架添加到项目中,导入:#import <MobileCoreServices/MobileCoreServices.h> 。不然后出现错误使用未声明的标识符 'kUTTypeMovie'
swift 中import MobileCoreServices
//选中图片,保存图片或视频到系统相册
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { //取消选择 picker.dismiss(animated: true) { } let image:UIImage=info[UIImagePickerControllerOriginalImage] as! UIImage//UIImagePickerControllerEditedImage(获得编辑后的图片)--UIImagePickerControllerOriginalImage(获得原形图片)// //缩小图片 let newSize=CGSize.init(width: 100, height: 100) UIGraphicsBeginImageContext(newSize)image.draw(in: CGRect.init(x: 0, y: 0, width: newSize.width, height: newSize.height)) let newImage:UIImage=UIGraphicsGetImageFromCurrentImageContext()! UIGraphicsEndImageContext(); // 图片类型 if info[UIImagePickerControllerMediaType] is kUTTypeImage{ // 这个是系统的方法,先来解释下各个参数: // // 1.image:将要保存的图片 // // 2.completionTarget:保存完毕后,回调方法所在的对象 // // 3.completionSelector:保存完毕后,回调的方法 // // 4.contextInfo:可选参数 UIImageWriteToSavedPhotosAlbum(newImage!, self, #selector(image(image:didFinishSavingWithError:contextInfo:)), nil) }else if info[UIImagePickerControllerMediaType] is kUTTypeMovie{ //视频类型 let url = info[UIImagePickerControllerMediaURL] as! NSURL//视频的url let urlStr = url.path if UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(urlStr!) { //保存视频到相簿,注意也可以使用ALAssetsLibrary来保存 UISaveVideoAtPathToSavedPhotosAlbum(urlStr!, self, "video:didFinishSavingWithError:contextInfo:", nil) print("保存成功......") } } } //保存视频到系统相册 func video(videoPath: String, didFinishSavingWithError error: NSError, contextInfo info: AnyObject) { print("保存成功") } //保存到系统相册 func image(image:UIImage,didFinishSavingWithError error:NSError?,contextInfo:AnyObject) {
}
import Photos添加代理:UIImagePickerControllerDelegate,UINavigationControllerDelegate
func openCamera(){//打开相册 let pickImageController:UIImagePickerController=UIImagePickerController.init() if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.photoLibrary){ //获取相册权限 PHPhotoLibrary.requestAuthorization({ (status) in switch status { case .notDetermined: break case .restricted://此应用程序没有被授权访问的照片数据 break case .denied://用户已经明确否认了这一照片数据的应用程序访问 break case .authorized://已经有权限 //跳转到相机或者相册 pickImageController.delegate=self as UIImagePickerControllerDelegate & UINavigationControllerDelegate pickImageController.allowsEditing = true pickImageController.sourceType = UIImagePickerControllerSourceType.photoLibrary; //弹出相册页面或相机 self.present(pickImageController, animated: true, completion: { }) break } }) } //打开相机拍照 if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera){ AVCaptureDevice.requestAccess(for: AVMediaType.video, completionHandler: { (ist) in let status:AVAuthorizationStatus=AVCaptureDevice.authorizationStatus(for: AVMediaType.video) if status==AVAuthorizationStatus.authorized {//获得权限 //跳转到相机或者相册 pickImageController.delegate=self as UIImagePickerControllerDelegate & UINavigationControllerDelegate pickImageController.allowsEditing = true pickImageController.sourceType = UIImagePickerControllerSourceType.camera; //弹出相册页面或相机 self.present(pickImageController, animated: true, completion: { }) }else if (status==AVAuthorizationStatus.denied)||(status==AVAuthorizationStatus.restricted) { } }) } } //去设置权限 func gotoSetting(){ let alertController:UIAlertController=UIAlertController.init(title: "去设置", message: "设置-》通用-》", preferredStyle: UIAlertControllerStyle.alert) let sure:UIAlertAction=UIAlertAction.init(title: "去开启权限", style: UIAlertActionStyle.default) { (ac) in let url=URL.init(string: UIApplicationOpenSettingsURLString) if UIApplication.shared.canOpenURL(url!){ UIApplication.shared.open(url!, options: [:], completionHandler: { (ist) in }) } } alertController.addAction(sure) self.present(alertController, animated: true) { } }要将MobileCoreServices 框架添加到项目中,导入:#import <MobileCoreServices/MobileCoreServices.h> 。不然后出现错误使用未声明的标识符 'kUTTypeMovie'
swift 中import MobileCoreServices
//选中图片,保存图片或视频到系统相册
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { //取消选择 picker.dismiss(animated: true) { } let image:UIImage=info[UIImagePickerControllerOriginalImage] as! UIImage//UIImagePickerControllerEditedImage(获得编辑后的图片)--UIImagePickerControllerOriginalImage(获得原形图片)// //缩小图片 let newSize=CGSize.init(width: 100, height: 100) UIGraphicsBeginImageContext(newSize)image.draw(in: CGRect.init(x: 0, y: 0, width: newSize.width, height: newSize.height)) let newImage:UIImage=UIGraphicsGetImageFromCurrentImageContext()! UIGraphicsEndImageContext(); // 图片类型 if info[UIImagePickerControllerMediaType] is kUTTypeImage{ // 这个是系统的方法,先来解释下各个参数: // // 1.image:将要保存的图片 // // 2.completionTarget:保存完毕后,回调方法所在的对象 // // 3.completionSelector:保存完毕后,回调的方法 // // 4.contextInfo:可选参数 UIImageWriteToSavedPhotosAlbum(newImage!, self, #selector(image(image:didFinishSavingWithError:contextInfo:)), nil) }else if info[UIImagePickerControllerMediaType] is kUTTypeMovie{ //视频类型 let url = info[UIImagePickerControllerMediaURL] as! NSURL//视频的url let urlStr = url.path if UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(urlStr!) { //保存视频到相簿,注意也可以使用ALAssetsLibrary来保存 UISaveVideoAtPathToSavedPhotosAlbum(urlStr!, self, "video:didFinishSavingWithError:contextInfo:", nil) print("保存成功......") } } } //保存视频到系统相册 func video(videoPath: String, didFinishSavingWithError error: NSError, contextInfo info: AnyObject) { print("保存成功") } //保存到系统相册 func image(image:UIImage,didFinishSavingWithError error:NSError?,contextInfo:AnyObject) {
}
相关文章推荐
- android 调用系统相册选取照片或者打开相机拍照获取图片
- android 调用系统相册选取照片或者打开相机拍照获取图片,返回时为什么会回到home界面或者到前一个界面,然后才会跳转到当前activity
- android 调用系统相册选取照片或者打开相机拍照获取图片,返回时回到手机桌面,然后才会跳转到当前activity
- 调用系统相机拍照、调用相册、调用系统录像并获取缩略图
- android 调用系统相册选取照片或者打开相机拍照获取图片,返回时回到手机桌面,然后才会跳转到当前activity
- android 调用系统相册选取照片或者打开相机拍照获取图片
- Android打开系统相机、相册,并进行裁剪图片的工具类CameraUtil,以及相册获取图片注意要点。
- Android 调用系统相机拍照保存以及调用系统相册的方法
- Android 调用系统相机拍照保存以及调用系统相册的方法
- iOS开发swift如何调用系统相册和相机获取图片设置用户头像
- Android 调用系统相机拍照保存以及调用系统相册的方法
- Android 调用系统相机拍照保存以及调用系统相册的方法
- Android 调用系统相机拍照保存以及调用系统相册的方法
- 打开相机拍照和从本地相册获取图片以及图片的剪切
- iOS 中 FaceID、相机、相册、定位、NFC、通讯录、蓝牙、健康等权限的查询和获取(swift)
- 调用系统相机拍照获取图片或者从相册中调用本地图片显示问题
- Android拍照和从相册获取图片(解决android7.0打开相机崩溃的问题),同时也解决了拍完照后图片方向不正的问题
- Android打开系统拍照&相册获取头像
- Android拍照和从相册获取图片(解决android7.0打开相机崩溃的问题),同时也解决了拍完照后图片方向不正的问题
- Android 调用系统相机拍照保存以及调用系统相册的方法