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链接:点击打开链接

时间: 2024-08-08 04:40:44

iOS9中,swift判断相机,相册权限,选取图片为头像的相关文章

相机权限和相册权限 使用block 判断

 .h文件中 + (void)camaraCanuse:(QTCamaraCanuse)camaraCanUse camaraNotCanuse:(QTCamaraNotCanuse)camaraNotCanUse showAlert:(BOOL)showAlert showMsg:(NSString*)showMsg; +(void)photoCanuse:(QTPhotoCanuse)photoCanUse photoNotCanuse:(QTPhotoNotCanuse)photoNotC

iOS相机权限、相册权限、定位权限判断

1.判断用户是否有权限访问相册 #import <AssetsLibrary/AssetsLibrary.h> ALAuthorizationStatus author = [ALAssetsLibraryauthorizationStatus]; if (author == kCLAuthorizationStatusRestricted || author ==kCLAuthorizationStatusDenied){ //无权限 } typedef enum { kCLAuthoriz

判断是否有权限访问相机,相册,定位

1.判断用户是否有权限访问相册 #import <AssetsLibrary/AssetsLibrary.h> ALAuthorizationStatus author = [ALAssetsLibraryauthorizationStatus]; if (author == kCLAuthorizationStatusRestricted || author ==kCLAuthorizationStatusDenied) { //无权限 } typedef enum { kCLAuthori

Android中通过访问本地相册或者相机设置用户头像

目前几乎所有的APP在用户注册时都会有设置头像的需求,大致分为三种情况: (1)通过获取本地相册的图片,经过裁剪后作为头像. (2)通过启动手机相机,现拍图片然后裁剪作为头像. (3)在APP中添加一些自带的头像资源,供用户选择(不够人性化,目前很少使用). 这次我们简单介绍下通过获取本地相册以及相机拍摄的方法设置头像,实现思路如下: (1)通过startActivityForResult方法,分别传递调用系统相册的Intent和调用相机拍照的Intent来做选择 (2)调用Android系统中

iOS10 相机相册等权限的使用、检测并引导用户开启权限

<!-- 相册 --> <key>NSPhotoLibraryUsageDescription</key> <string>App需要您的同意,才能访问相册</string> <!-- 相机 --> <key>NSCameraUsageDescription</key> <string>App需要您的同意,才能访问相机</string> <!-- 麦克风 --> <ke

swift-判断是否已获得相机、相册权限

// 相机权限 func isRightCamera() -> Bool { let authStatus = AVCaptureDevice.authorizationStatus(forMediaType: AVMediaTypeVideo) return authStatus != .restricted && authStatus != .denied } // 相册权限 func isRightPhoto() -> Bool { let authStatus = AL

iOS 判断应用是否有使用相机的权限

iOS  判断应用是否有使用相机的权限 NSString *mediaType = AVMediaTypeVideo; AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:mediaType]; if(authStatus == ALAuthorizationStatusRestricted || authStatus == ALAuthorizationStatusDenied)

Android设置头像,手机拍照或从本地相册选取图片作为头像

 [Android设置头像,手机拍照或从本地相册选取图片作为头像] 像微信.QQ.微博等社交类的APP,通常都有设置头像的功能,设置头像通常有两种方式: 1,让用户通过选择本地相册之类的图片库中已有的图像,裁剪后作为头像. 2,让用户启动手机的相机拍照,拍完照片后裁剪,然后作为头像. 我现在写一个简单的完整代码例子,说明如何在Android中实现上述两个头像设置功能. MainActivity.java文件: package zhangpgil.photo; import java.io.F

iOS开发实战——摄像头与相册权限获取逻辑优化

在实际项目中,我们经常需要访问设备的摄像头或者相册,当第一次安装某个App的时候,系统便会弹出授权对话框,要求用户做出是否授权的判断.整体逻辑比较简单,但是在使用过程中需要对用户体验进行优化,否则会出现bug.该博客的示例代码已经上传至 https://github.com/chenyufeng1991/AuthorityOfCameraAndPhoto . 首先我先描述一下出现的问题.我以访问相册为例,实现代码如下: - (void)photoBtnPressed:(id)sender { /