分享

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

 求知665 2014-10-18

#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;

    }

}


    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多