iPhone拍攝功能異常強大,我們需要知道如何操作攝像頭。
實現拍照、錄像和顯示系統照片
-----------------------
預備知識:
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:當用戶取消操作時調用
根據實踐當用戶成功操作或者放棄操作時,我們應該退出圖片選擇器。