1.首先
引入#import 系统类库AVFoundation/AVFoundation.h
签俩个系统代理UIImagePickerControllerDelegate,UINavigationControllerDelegate
2.利用系统弹窗UIActionSheet
-(void)changeBackground
{
NSLog(@"更换背景代理");
UIActionSheet *changeSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"打开照相机",@"从手机相册获取", nil];
[changeSheet showInView:self.view];
}
#pragma mark UIActionSheet 点击按钮触发的方法
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex NS_DEPRECATED_IOS(2_0, 8_3)
{
//呼出的菜单按钮点击后的响应
if (buttonIndex == actionSheet.cancelButtonIndex)
{
NSLog(@"取消");
}
if (buttonIndex == 0) {
[self takePhoto];
}
if (buttonIndex == 1){
[self LocalPhoto];
}
}
-(void)takePhoto
{
//判断相机是否能够使用
AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];
UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = sourceType;
if (status == AVAuthorizationStatusAuthorized) {
/********** 已经授权 可以打开相机 ***********/
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
[self presentViewController:_picker animated:YES completion:^{
}];
}
/********** 已经授权 可以打开相机 ***********/
}else if (status == AVAuthorizationStatusNotDetermined){
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeAudio completionHandler:^(BOOL granted) {
if (granted) {
//第一次用户接受
[self presentViewController:_picker animated:YES completion:nil];
}else{
//用户拒绝
}
}];
}else if (status == AVAuthorizationStatusRestricted){
UIAlertView *aview = [[UIAlertView alloc] initWithTitle:@"提示" message:@"您的相机权限受限" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
[aview show];
}else if (status == AVAuthorizationStatusDenied){
UIAlertView *aview= [[UIAlertView alloc] initWithTitle:@"提示" message:@"请在设备的\"设置-隐私-相机\"中允许访问相机。" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
[aview show];
}
}
//有的时候打开相机 相机只显示黑色没有图像 定义下方属性解决
控制台报错 Snapshotting a view that has not been rendered results in an empty snapshot. Ensure your view has been rendered at least once before snapshotting or snapshot after screen updates
self.modalPresentationStyle=UIModalPresentationOverCurrentContext;
#pragma mark
#pragma mark 打开本地相册
-(void)LocalPhoto
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
picker.delegate = self;
//设置选择后的图片可被编辑
picker.allowsEditing = YES;
[self presentViewController:picker animated:YES completion:^{
}];
}
#pragma mark
#pragma mark 当选择一张图片进入这里
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(nullable NSDictionary<NSString *,id> *)editingInfo NS_DEPRECATED_IOS(2_0, 3_0)
{
[picker dismissViewControllerAnimated:YES completion:^{
}];
_headview.imgBgView.image = image;
//将照片存到媒体库
UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
}
#pragma mark - 照片存到本地后的回调
- (void)image:(UIImage*)image didFinishSavingWithError:(NSError*)error contextInfo:(void*)contextInfo{
if (!error) {
NSLog(@"存储成功");
} else {
NSLog(@"存储失败:%@", error);
}
}
下面是相册
#pragma mark
#pragma mark 打开本地相册
-(void)LocalPhoto
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
picker.delegate = self;
//设置选择后的图片可被编辑
picker.allowsEditing = YES;
//相册的权限
PHAuthorizationStatus photoAuthorStatus = [PHPhotoLibrary authorizationStatus];
if (photoAuthorStatus == PHAuthorizationStatusAuthorized) {
NSLog(@"Authorized");
[self presentViewController:picker animated:YES completion:nil];
}else if (photoAuthorStatus == PHAuthorizationStatusDenied){
NSLog(@"Denied");
UIAlertView *aview= [[UIAlertView alloc] initWithTitle:@"提示" message:@"请在设备的\"设置-隐私-相册\"中允许访问相册。" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
[aview show];
}else if (photoAuthorStatus == PHAuthorizationStatusNotDetermined){
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
if (status == PHAuthorizationStatusAuthorized) {
NSLog(@"Authorized");
}else{
NSLog(@"Denied or Restricted");
}
}];
NSLog(@"not Determined");
}else if (photoAuthorStatus == PHAuthorizationStatusRestricted){
NSLog(@"Restricted");
}
}
以上是我选择系统相册和相机的使用!希望能对你们有帮助。