iOS9中,swift判断相机,相册权限,选取图片为头像
在iOS7以后要打开手机摄像头或者相册的话都需要权限,在iOS9中更是更新了相册相关api的调用
首先新建一个swift工程,在SB中放上一个按钮,并在viewController中拖出点击事件
ok!按钮和事件设置好以后,我们来引入要用到的库,判断摄像头权限,需要引入AVFoundation.framework,搜索并进行添加
在ViewController中 import AVFoundation
并遵循以下几个代理UIImagePickerControllerDelegate,UIActionSheetDelegate,UINavigationControllerDelegate
声明我们需要的变量
var img :UIImageView!
var sheet:UIAlertController!
var sourceType = UIImagePickerControllerSourceType.PhotoLibrary //将sourceType赋一个初值类型,防止调用时不赋值出现崩溃
在viewDidLoad中:
override func viewDidLoad() {
super.viewDidLoad()
img = UIImageView(frame: CGRectMake(20, 120, 100, 100))
self.view.addSubview(img)
}
由于我们选择相册或者打开摄像头以后进行图片编辑的操作是一样的,所以我们将这段代码封装到open方法里面
// 打开图库或相机
func open(){
let imagePickerController:UIImagePickerController = UIImagePickerController()
imagePickerController.delegate = self
imagePickerController.allowsEditing = true//true为拍照、选择完进入图片编辑模式
imagePickerController.sourceType = sourceType
self.presentViewController(imagePickerController, animated: true, completion:{
})
}
然后我们再将判断相册权限和摄像头权限的代码封装到各自的方法中进行调用
/**
判断相机权限
- returns: 有权限返回true,没权限返回false
*/
func cameraPermissions() -> Bool{
let authStatus:AVAuthorizationStatus = AVCaptureDevice.authorizationStatusForMediaType(AVMediaTypeVideo)
if(authStatus == AVAuthorizationStatus.Denied || authStatus == AVAuthorizationStatus.Restricted) {
return false
}else {
return true
}
}
(相机权限的判断和OC基本一致,只是方法调用方法变化了而已)
/**
判断相册权限
- returns: 有权限返回ture, 没权限返回false
*/
func PhotoLibraryPermissions() -> Bool {
let library:PHAuthorizationStatus = PHPhotoLibrary.authorizationStatus()
if(library == PHAuthorizationStatus.Denied || library == PHAuthorizationStatus.Restricted){
return false
}else {
return true
}
}
(相册权限判断这里在iOS9之前都是用的AssetsLibrary库,在iOS9之后引用的是Photos库了,虽然依然可以调用AssetsLibrary库进行判断,但会有警告Photos库的引用,import
Photos就行)
接下来就是在前面拖出的按钮事件中进行代码编写了:
@IBAction func picker(sender: AnyObject) {
//判断设置是否支持图片库和相机
if(UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera)
&&
UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.PhotoLibrary)){
sheet = UIAlertController(title: nil, message: "选择获取头像方式", preferredStyle: .ActionSheet)
//取消
let cancelAction = UIAlertAction(title: "取消", style: .Cancel, handler: {(action) in
print("取消")
})
sheet.addAction(cancelAction)
//相册
let OKAction = UIAlertAction(title: "相册", style: .Default, handler: {(action) in
if(self.PhotoLibraryPermissions() == true){
self.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
self.open()
}else{
//弹出提示框
self.sheet = UIAlertController(title: nil, message: "请在设置中打开相册权限", preferredStyle: .Alert)
let tempAction = UIAlertAction(title: "确定", style: .Cancel) { (action) in
print("取消")
}
self.sheet.addAction(tempAction)
self.presentViewController(self.sheet, animated: true, completion: nil)
}
})
sheet.addAction(OKAction)
//摄像头
let destroyAction = UIAlertAction(title: "摄像头", style: .Default, handler: { (action) in
if(self.cameraPermissions() == true){
self.sourceType = UIImagePickerControllerSourceType.Camera
self.open()
}else {
//弹出提示框
self.sheet = UIAlertController(title: nil, message: "请在设置中打开摄像头权限", preferredStyle: .Alert)
let tempAction = UIAlertAction(title: "确定", style: .Cancel) { (action) in
}
self.sheet.addAction(tempAction)
self.presentViewController(self.sheet, animated: true, completion: nil)
}
})
sheet.addAction(destroyAction)
}
self.presentViewController(self.sheet, animated: true, completion: nil)
}
最后
// 取消图片选择操作
func imagePickerControllerDidCancel(picker:UIImagePickerController)
{
self.dismissViewControllerAnimated(true, completion: nil)
}
// 选择完图片操作
func imagePickerController(picker: UIImagePickerController,
didFinishPickingImage image: UIImage!, editingInfo: [NSObject :
AnyObject]!) {
img.image = image
self.dismissViewControllerAnimated(true, completion: nil)
}
注:因前面判断了相机判断,demo只能在真机上运行
附上demo链接:点击打开链接
iOS9中,swift判断相机,相册权限,选取图片为头像的更多相关文章
- ios 审核未通过 相机相册权限问题
苹果提交审核被打回来 附加的说明如下: We noticed that your app requests the user’s consent to access their camera but ...
- 009android初级篇之APP中使用系统相机相册等集成应用
android应用中使用相机功能,大致有两种方式实现: 直接调用系统内部的相机程序,显示的也是系统预设的界面(简单,只有简单的拍照功能): 自己去implement一个相机程序(不难,较具备弹性,但相 ...
- iOS 判断是否有权限访问相机,相册
1.判断用户是否有权限访问相册 #import <AssetsLibrary/AssetsLibrary.h> ALAuthorizationStatus author =[ALAsset ...
- iOS相机权限、相册权限、定位权限判断
1.判断用户是否有权限访问相册 #import <AssetsLibrary/AssetsLibrary.h> ALAuthorizationStatus author = [ALAsse ...
- iOS 判断相机权限是否被限制,判断相机是否可以使用
判断相机权限是否被限制 需要导入 AVFoundation 类 [objc] view plain copy #import <AVFoundation/AVFoundation.h> ...
- iOS10 相机相册等权限的使用、检测并引导用户开启权限
<!-- 相册 --> <key>NSPhotoLibraryUsageDescription</key> <string>App需要您的同意,才能访问 ...
- Android中通过访问本地相册或者相机设置用户头像
目前几乎所有的APP在用户注册时都会有设置头像的需求,大致分为三种情况: (1)通过获取本地相册的图片,经过裁剪后作为头像. (2)通过启动手机相机,现拍图片然后裁剪作为头像. (3)在APP中添加一 ...
- iPhone中如何判断当前相机是否可用
UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera; if (![UIImag ...
- swift调用相机和相册
简单实现swift调用相机和相册的功能,分享代码与学习swift的童鞋共同进步 import UIKit class ViewController: UIViewController,UIImageP ...
随机推荐
- mysql 强制修改密码
mysql忘记密码时强制修改步骤如下: 1.用命令编辑配置文件/etc/my.cnf 2.添加一条语句使其变为不用密码就能进入的状态 skip-grant-tables 3.保存并退出,然后再命令行输 ...
- 个人网站html5雪花飘落代码JS特效下载
如何给自己的网站/页面添加雪花代码.特效呢?有的网站配合自己的主题模板添加雪花飘落效果挺好看的.特别是与冬天季节相关的主题,很多的博客空间都加了雪花的效果.在网上搜索了几种雪花效果,做了简单的修改,在 ...
- webpack打包大概流程
webpack 步骤 1. 新建一个webpack.config.prod.js 2. 压缩bundle.js和index.html //设置为生产环境 new webpack.DefinePlugi ...
- Data内置对象
1.内置对象 Date 日期对象 2.创建日期对象 2.1 根据当前的系统时间来创建日期对象. var date1 = new Date(); //a.输出日期对象的信息 console.log(da ...
- hdu2282 Chocolate 完美匹配 + 拆点
题意: N个箱子排成一个圈,所有的箱子里的巧克力的数量加起来不大于N,每次可以把箱子里的巧克力向旁边的箱子转移(两个方向),问要让每个箱子里的巧克力不大于1的最小步数. 分析: 把巧克力大于1的箱子拆 ...
- SqlServer与MySql语法比较
1.复制表(包括表结构.表数据) SqlServer: Select * into user_copy from user MySql: CREATE TABLE user_copy LIKE use ...
- Warning:关于_CRT_SECURE_NO_WARNINGS
Warning 1 warning C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s in ...
- (转)基于MVC4+EasyUI的Web开发框架形成之旅--总体介绍
http://www.cnblogs.com/wuhuacong/p/3281103.html 最近花了很多时间在重构和进一步提炼Winform开发框架的工作上,加上时不时有一些项目的开发工作,我博客 ...
- Mysql错误:#1054 - Unknown column '字段名' in 'field list'
# 1054 - Unknown column '字段名' in 'field list' 第一个就是你的表中没有这个字段 另一个就是你的这个字段前后可能有空格!!!,去掉空格即可!
- indy10中idtcpclient的使用问题[和大华电子称数据交换]
在实际事务应用中,多次打开server进行大写.其中遇到一些问题,由于时间关系,没有好好整理,虽然问题解决了, 但原因和其他方法没有去进一步测试. 1.每个单元用本地TidTCPClient变量连接s ...