{
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;
}
原文版权归作者所有,转载请注明来源