UIActionSheet的使用和系统相机的相关调用

#pragma mark ------ 调用系统相机 ------

- (IBAction)cameraButton:(id)sender {

    

    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"照相机" otherButtonTitles:@"图片库",@"闪光灯", nil];//UIActionSheet初始化,并设置delegate

    actionSheet.actionSheetStyle = UIActionSheetStyleDefault;

    [actionSheet showFromRect:self.view.bounds inView:self.view animated:YES]; // actionSheet弹出位置

}


#pragma mark -UIActionSheetDelegate

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex

{

    switch (buttonIndex) {

        case 0:

        {

            NSLog(@"打开系统照相机");

            if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {

                UIImagePickerController *picker = [[UIImagePickerController alloc] init];

                picker.delegate = self;//设置UIImagePickerController的代理,同时要遵循UIImagePickerControllerDelegateUINavigationControllerDelegate协议

                picker.allowsEditing = YES;//设置拍照之后图片是否可编辑,如果设置成可编辑的话会在代理方法返回的字典里面多一些键值。PS:如果在调用相机的时候允许照片可编辑,那么用户能编辑的照片的位置并不包括边角。

                picker.sourceType = UIImagePickerControllerSourceTypeCamera;//UIImagePicker选择器的类型,UIImagePickerControllerSourceTypeCamera调用系统相机

                [self presentViewController:picker animated:YES completion:nil];

            }

            else{

                //如果当前设备没有摄像头

                UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:@"哎呀,当前设备没有摄像头。" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];

                [alertView show];

            }

            break;

        }

        case 1:

        {

            NSLog(@"打开系统图片库");

            if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {

                UIImagePickerController * picker = [[UIImagePickerController alloc]init];

                picker.delegate = self;

                picker.allowsEditing = YES;//是否可以对原图进行编辑

                

                //打开相册选择照片

                picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

                [self presentViewController:picker animated:YES completion:nil];

            }

            else{

                UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:@"图片库不可用" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];

                [alertView show];

            }

            break;

        }

        case 2:

        {

            NSLog(@"调用系统闪关灯");

            

            if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])

            {

                device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; // 返回用于捕获视频数据的设备(摄像头)

                if (![device hasTorch]) {

                    NSLog(@"没有闪光灯");

                }else{

                    [device lockForConfiguration:nil]; // 请求独占设备的硬件性能

                    if (device.torchMode == AVCaptureTorchModeOff) {

                        [device setTorchMode: AVCaptureTorchModeOn]; // 打开闪光灯

                    }

                }

            }

            else

            {

                //如果当前设备没有摄像头

                UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:@"哎呀,当前设备没有摄像头。" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];

                [alertView show];

            }

            

            break;

        }

        case 3:

        {

            NSLog(@"取消");

            break;

        }

        default:

            break;

    }

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值