17 | 摄像头和相册

iPhone拍摄功能异常强大,我们需要知道如何操作摄像头。

实现拍照、录像和显示系统照片

链接:操作摄像头打开相册demo

-----------------------

预备知识:

UIImagePickerController | 图片选择器(应用程序通过它来操作摄像头和照片库)的对应类

使用之前先判断是否支持拍照、录像或者照片库这是很有必要的,比如某些版本的iTouch是不支持camera的。通常设置allowsEditing属性为YES,因为大部分的用户希望每次拍完照片就可以对照片进行一些简单的处理。

sourceType这个属性决定图像选择器是打开摄像头拍摄照片,还是录制一段视频,还是打开相册选取某个文件。

//
//  ViewController.m
//  ImagePicker
//
//  Created by Wu on 16/4/6.
//  Copyright © 2016年 Wu. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>//遵从协议
@property(nonatomic , assign)BOOL cameraAvailable;
@property(nonatomic , assign)BOOL photoesLibraryAvailable;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //判断设备是否支持摄像头
    self.cameraAvailable = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];
    //判断设备是否支持照片库
    self.photoesLibraryAvailable = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary];
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc]init];//创建实例
    imagePickerController.delegate = self;
    imagePickerController.allowsEditing = YES;//容许编辑
    imagePickerController.cameraDevice = UIImagePickerControllerCameraDeviceFront;//选择camera设备为前置摄像头
    imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;//选择图像源
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
@end

实际例子:|  拍照必须在真机下才能测试

//
//  ViewController.m
//  ImagePicker
//
//  Created by Wu on 16/4/6.
//  Copyright © 2016年 Wu. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>//遵从协议
@property(nonatomic , assign)BOOL cameraAvailable;
@property(nonatomic , assign)BOOL photoesLibraryAvailable;
@property(nonatomic , strong)UIImagePickerController *imagePickerController;

@property (strong, nonatomic) IBOutlet UIImageView *imageView;
@property (strong, nonatomic) IBOutlet UIButton *camera;
@property (strong, nonatomic) IBOutlet UIButton *photoLib;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.imageView.contentMode = UIViewContentModeScaleAspectFit;
    
    //判断设备是否支持摄像头
    self.cameraAvailable = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];
    //判断设备是否支持照片库
    self.photoesLibraryAvailable = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary];
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc]init];//创建实例
    imagePickerController.delegate = self;
    imagePickerController.allowsEditing = YES;//容许编辑

    self.imagePickerController = imagePickerController;
}

- (IBAction)takePhoto:(id)sender {
    if (self.cameraAvailable) {
        self.imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
        self.imagePickerController.cameraDevice = UIImagePickerControllerCameraDeviceFront;//选择camera设备为前置摄像头,sourceType必须已经为camera
        [self presentViewController:self.imagePickerController animated:YES completion:nil];
    }
}

- (IBAction)photoesLibrary:(id)sender {
    if (self.photoesLibraryAvailable) {
        self.imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        [self presentViewController:self.imagePickerController animated:YES completion:nil];
    }
}

#pragma mark- Delegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
//    self.imageView.image = [info objectForKey:UIImagePickerControllerOriginalImage];//获取原始照片
    self.imageView.image = [info objectForKey:UIImagePickerControllerEditedImage];//获取编辑后的照片
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
@end
代理方法:

didFinishPickingMediaWithInfo:当用户成功拍照或者从照片库中选择了一张图片后调用

DidCancel:当用户取消操作时调用

根据实践当用户成功操作或者放弃操作时,我们应该退出图片选择器。













發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章