1. 程式人生 > >iOS9中,swift判斷相機,相冊權限,選取圖片為頭像

iOS9中,swift判斷相機,相冊權限,選取圖片為頭像

返回 而已 頭像 iss 模式 會有 vid str 編輯模式

在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判斷相機,相冊權限,選取圖片為頭像