#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的代理,同时要遵循UIImagePickerControllerDelegate,UINavigationControllerDelegate协议
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;
}
}