iOS7 打開相機代碼

廢話少說,直接上代碼。。。。

#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];
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章