在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判断相机,相册权限,选取图片为头像的更多相关文章

  1. ios 审核未通过 相机相册权限问题

    苹果提交审核被打回来  附加的说明如下: We noticed that your app requests the user’s consent to access their camera but ...

  2. 009android初级篇之APP中使用系统相机相册等集成应用

    android应用中使用相机功能,大致有两种方式实现: 直接调用系统内部的相机程序,显示的也是系统预设的界面(简单,只有简单的拍照功能): 自己去implement一个相机程序(不难,较具备弹性,但相 ...

  3. iOS 判断是否有权限访问相机,相册

    1.判断用户是否有权限访问相册 #import <AssetsLibrary/AssetsLibrary.h> ALAuthorizationStatus author =[ALAsset ...

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

    1.判断用户是否有权限访问相册 #import <AssetsLibrary/AssetsLibrary.h> ALAuthorizationStatus author = [ALAsse ...

  5. iOS 判断相机权限是否被限制,判断相机是否可以使用

    判断相机权限是否被限制 需要导入   AVFoundation 类 [objc] view plain copy #import <AVFoundation/AVFoundation.h> ...

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

    <!-- 相册 --> <key>NSPhotoLibraryUsageDescription</key> <string>App需要您的同意,才能访问 ...

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

    目前几乎所有的APP在用户注册时都会有设置头像的需求,大致分为三种情况: (1)通过获取本地相册的图片,经过裁剪后作为头像. (2)通过启动手机相机,现拍图片然后裁剪作为头像. (3)在APP中添加一 ...

  8. iPhone中如何判断当前相机是否可用

    UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera; if (![UIImag ...

  9. swift调用相机和相册

    简单实现swift调用相机和相册的功能,分享代码与学习swift的童鞋共同进步 import UIKit class ViewController: UIViewController,UIImageP ...

随机推荐

  1. OpenCascade 边界表示法(BRep)

    转自 http://www.cppblog.com/eryar/archive/2013/08/20/202678.html Topology and Geometry in OpenCascade- ...

  2. 9.13[XJOI] NOIP训练32

    今日9.13 洛谷打卡:小吉(今天心情不错,决定取消密码) (日常记流水账) 上午 今天听说是鏼鏼的题目,题面非常的清真啊,也没有当初以为的爆零啊 T1 排排坐 非常非常清真的模拟或是结论题,再次将难 ...

  3. A - Kefa and First Steps

    Problem description Kefa decided to make some money doing business on the Internet for exactly n day ...

  4. 兼容各个浏览器的jquyer zclip复制文本插件 无效的解决办法

    项目中使用点击文本复制功能,用了这个兼容各个浏览器的插件,但是发现放在最前面正常,放到嵌套的html中就失效. 解决办法: <span style="position: relativ ...

  5. less常用方法

    最近在开发中使用了less,总结一下less一些常用的方法: 1.可以定义变量 SASS允许使用变量,所有变量以$开头. $blue : #1875e7; div { color : $blue; } ...

  6. Android ExpandableListView group的item有间距child间隔不变

    <ExpandableListView android:id="@+id/lv" android:layout_width="fill_parent" a ...

  7. ubuntu+win10双系统,调整分区大小后进入了emergency mode

    问题背景: 装了Ubuntu+win10双系统,在Ubuntu下面挂载了Windows的D盘.后来因为D空间不够,进入Windows压缩C盘分区,扩大了D盘.重启后无法启动Ubuntu,进入了emer ...

  8. Jenkins介绍-安装-部署...

    1.背景      大师Martin Fowler对持续集成是这样定义的:持续集成是一种软件开发实践,即团队开发成员经常集成他们的工作,通常每个成员每天至少集成一次,也就意味着每天可能会发生多次集成. ...

  9. (转)RabbitMQ学习之exchange总结

    http://blog.csdn.net/zhu_tianwei/article/details/53969674 前面介绍了几类exchange的作用,这个总结一下: direct:消息会被推送至绑 ...

  10. pymmseg 安装方法以及乱码解决

    pymmseg-cpp is a Python port of the rmmseg-cpp project. rmmseg-cpp is a MMSEG Chinese word segmentin ...