• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

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

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Swift-将String类型的数字转换成数字类型(支持十进制、十六进制) ...发布时间:2022-07-13
下一篇:
[Swift]DFS和BFS发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap