iOS上传头像, 相册权限,相册权限,拍照上传,相册选择图片,拍照页面语言设置,保存到相册...
2019獨角獸企業(yè)重金招聘Python工程師標準>>>
1. 權(quán)限
在打開相機拍照或者打開相冊選擇圖片之前, 有必要先判斷先是否有權(quán)限, 如果沒有權(quán)限應(yīng)該給個提示, 讓用戶自己去設(shè)置權(quán)限.
判斷是否有相機權(quán)限:
//首先需要導(dǎo)入頭文件: #import <AVFoundation/AVFoundation.h>// 判斷是夠有全向訪問相機 AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo]; if (authStatus == AVAuthorizationStatusRestricted || authStatus ==AVAuthorizationStatusDenied) {//無權(quán)限NSLog(@"沒有訪問相機權(quán)限");return; }判斷是否有相冊權(quán)限:
網(wǎng)上找了很多, 都是說用另一個類來判斷的, 叫什么名字忘了, 但是是不能用的, 正確姿勢是使用PHPhotoLibrary這個類.
//首先需要導(dǎo)入頭文件: #import <Photos/PHPhotoLibrary.h>// 判斷是否有訪問相冊的權(quán)限 PHAuthorizationStatus author = [PHPhotoLibrary authorizationStatus]; if (author == PHAuthorizationStatusRestricted || author ==PHAuthorizationStatusDenied){//無權(quán)限NSLog(@"沒有訪問相冊的權(quán)限");return; }2. 拍照或者使用相冊照片
從相機或者相冊選擇照片需要用到UIImagePickerController類.
從相機選擇照片:
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init]; imagePicker.delegate = self; imagePicker.allowsEditing = YES; imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera; [self presentViewController:imagePicker animated:YES completion:nil];從相冊選擇照片:
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init]; imagePicker.delegate = self; imagePicker.allowsEditing = YES; imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;[self presentViewController:imagePicker animated:YES completion:nil];3. 保存照片到相冊
拍照方式選擇照片時候, 一般需要將照片保存到本地相冊中, 保存相冊的代碼只有一個方法, 如下所示.
// 將拍照的圖片保存到本地UIImageWriteToSavedPhotosAlbum(originalImage, self, @selector(imageSavedToPhotosAlbum:didFinishSavingWithError:contextInfo:), nil);需要注意的是, 第一個參數(shù)是要保存的圖片, 第二個參數(shù)是要執(zhí)行方法的target, 第三個參數(shù)是selector, 第四個參數(shù)是傳參數(shù). 第三個參數(shù)selector, selector的方法名字一般是有參數(shù)的(測試寫無參數(shù)的, 保存時候回崩潰).
//此方法一般是三個參數(shù), 否則容易出錯. - (void)imageSavedToPhotosAlbum:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {NSString *message = @"呵呵";if (!error) {message = @"成功保存到相冊";}else{message = [error description];}NSLog(@"message is %@",message); }轉(zhuǎn)載于:https://my.oschina.net/whforever/blog/702761
總結(jié)
以上是生活随笔為你收集整理的iOS上传头像, 相册权限,相册权限,拍照上传,相册选择图片,拍照页面语言设置,保存到相册...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: “文心一言”领路,百度全面提速
- 下一篇: OPPO回应将裁撤电视业务线:业务正常运