iOS7 打开相机代码

来源:互联网 发布:java安装后怎么启动 编辑:程序博客网 时间:2024/04/29 04:38

废话少说,直接上代码。。。。

#import "ViewController.h"#import <AVFoundation/AVFoundation.h>@interface ViewController () <UIImagePickerControllerDelegate, UINavigationControllerDelegate>@property (weak, nonatomic) UIImageView *imageView;@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    UIImageView *imageView = [[UIImageView alloc] init];    imageView.frame = CGRectMake(100, 100, 100, 100);    self.imageView = imageView;    [self.view addSubview:imageView];        UIButton *button = [UIButton buttonWithType:UIButtonTypeContactAdd];    button.frame = CGRectMake(100, 50, 100, 100);    [button addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];    [self.view addSubview:button];}- (void)click{    if ([[UIDevice currentDevice].systemVersion floatValue] >= 7.0) {        // 提醒用户在哪里可以设置        AVAuthorizationStatus state = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];        if (state == AVAuthorizationStatusRestricted || state == AVAuthorizationStatusDenied) {            UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:@"请在iPhone的“设置-隐私-相机”选项中允许XXX访问您的手机相机" preferredStyle:UIAlertControllerStyleAlert];            UIAlertAction *alertAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];            [alertController addAction:alertAction];            [self presentViewController:alertController animated:YES completion:nil];        }    }    UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {        UIImagePickerController *pickerController = [[UIImagePickerController alloc] init];        pickerController.delegate = self;        pickerController.sourceType = sourceType;        [self presentViewController:pickerController animated:YES completion:nil];    } else {        // 把UIAlertView, UIActionSheet集成UIAlertController        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:@"无法启动照相机" preferredStyle:UIAlertControllerStyleAlert];//        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:@"无法启动照相机" preferredStyle:UIAlertControllerStyleActionSheet];        UIAlertAction *alertAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];        [alertController addAction:alertAction];        [self presentViewController:alertController animated:YES completion:nil];        /**        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:@"无法启动照相机" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];        [alertView show];        */    }}#pragma mark - 最新- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{    [picker dismissViewControllerAnimated:YES completion:nil];    self.imageView.image = info[UIImagePickerControllerOriginalImage];}#pragma mark - NS_DEPRECATED_IOS(2_0, 3_0)过期- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary<NSString *,id> *)editingInfo{    [picker dismissViewControllerAnimated:YES completion:nil];    self.imageView.image = image;}- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {    [self dismissViewControllerAnimated:YES completion:nil];}
0 0
原创粉丝点击