iOS 拍照与相片选择,在模拟器上拍照时做一个友好提示

当在iOS应用中使用UIImagePickerController进行拍照或选择相册照片时,本文介绍了如何在模拟器上针对无相机权限给出友好提示,并提供在用户未授权相机权限时的处理方法。同时,详细展示了在用户点击不同选项时调用相机或相册的代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

- (IBAction)singleTapAction:(id)sender
{
    UIActionSheet *sheet = [[UIActionSheet alloc]init];
    [sheet addButtonWithTitle:@"拍照"];
    [sheet addButtonWithTitle:@"从相册选择"];
    [sheet addButtonWithTitle:@"取消"];
    [sheet setCancelButtonIndex:2];
    [sheet setDelegate:self];
    sheet.tag = 255;
    
    [sheet showInView:self.view];
}


#pragma mark - actionsheet delegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (actionSheet.tag == 101) {
        UIImagePickerController *pickController = [[UIImagePickerController alloc] init];
        pickController.delegate = self;
        [pickController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
        if (buttonIndex == 0) {
            AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
            if ((authStatus == AVAuthorizationStatusRestricted || authStatus == AVAuthorizationStatusDenied) && iOS7Later) {
                // 无相机权限 做一个友好的提示
                UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"无法使用相机" message:@"请在iPhone的""设置-隐私-相机""中允许访问相机" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"设置", nil];
                [alert show];
            } else { // 调用相机
//                UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
                if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]) {
                    pickController.sourceType = UIImagePickerControllerSourceTypeCamera;
                    [self presentViewController:pickController animated:YES completion:nil];
                } else {
                    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"模拟器里不支持拍照,请在真机上测试!" delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];
                    [NSTimer scheduledTimerWithTimeInterval:3.0f target:self selector:@selector(timerFireMethod:) userInfo:alert repeats:YES];
                    [alert show];
                }
            }
            
        }
        if (buttonIndex == 1) {
            pickController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            [self presentViewController:pickController animated:YES completion:nil];
        }
        
    }
}

-(void)timerFireMethod:(NSTimer*)theTimer//弹出框
{
    UIAlertView *promptAlert = (UIAlertView*)[theTimer userInfo];
    [promptAlert dismissWithClickedButtonIndex:0 animated:NO];
    promptAlert =NULL;
}

原文版权归作者所有,转载请注明来源

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值