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

来源:互联网 发布:2g和3g网络标志的区别 编辑:程序博客网 时间:2024/05/16 06:35

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

- (IBAction)cameraButton:(id)sender {

    

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

    actionSheet.actionSheetStyle =UIActionSheetStyleDefault;

    [actionSheet showFromRect:self.view.boundsinView:self.viewanimated:YES];// actionSheet弹出位置

}


#pragma mark -UIActionSheetDelegate

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

{

   switch (buttonIndex) {

       case 0:

        {

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

            if ([UIImagePickerControllerisSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {

                UIImagePickerController *picker = [[UIImagePickerControlleralloc] init];

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

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

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

                [selfpresentViewController:picker animated:YEScompletion:nil];

            }

           else{

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

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

                [alertViewshow];

            }

           break;

        }

       case 1:

        {

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

            if ([UIImagePickerControllerisSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {

                UIImagePickerController * picker = [[UIImagePickerControlleralloc]init];

                picker.delegate =self;

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

                

               //打开相册选择照片

                picker.sourceType =UIImagePickerControllerSourceTypePhotoLibrary;

                [selfpresentViewController:picker animated:YEScompletion:nil];

            }

           else{

                UIAlertView *alertView = [[UIAlertViewalloc] initWithTitle:nilmessage:@"图片库不可用" delegate:nilcancelButtonTitle:@"确定"otherButtonTitles:nil];

                [alertViewshow];

            }

           break;

        }

       case 2:

        {

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

            

            if ([UIImagePickerControllerisSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])

            {

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

               if (![devicehasTorch]) {

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

                }else{

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

                    if (device.torchMode ==AVCaptureTorchModeOff) {

                        [devicesetTorchMode: AVCaptureTorchModeOn];// 打开闪光灯

                    }

                }

            }

           else

            {

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

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

                [alertViewshow];

            }

            

           break;

        }

       case 3:

        {

           NSLog(@"取消");

           break;

        }

       default:

           break;

    }

}


0 0