IOS开发调用系统相机和打开闪光灯
2015-09-24 18:32
507 查看
一、首先调用接口前,我们需要先判断当前设备是否支持UIImagePickerController,用isSourceTypeAvailable:来判断是否可用
二、查看符合的媒体类型,这个时候我们调用availableMediaTypeForSourceType:判断
在调用UIImagePickerController时我们需要加入他的两个代理方法:
UINavigationControllerDelegate和UIImagePickerControllerDelegate,在调用摄像头的时候还可以调闪光灯,一会代码里有。
要调用闪光灯需要先建一个AVCaptureSession类的实例对象:
[java] view
plaincopy
#import <UIKit/UIKit.h>
//调用闪光灯调用框架
#import <AVFoundation/AVFoundation.h>
@interface CameraViewController : UIViewController<UINavigationControllerDelegate, UIImagePickerControllerDelegate>
{
AVCaptureSession * _AVSession;//调用闪光灯的时候创建的类
}
@property(nonatomic,retain)AVCaptureSession * AVSession;
@end
在.m的- (void)viewDidLoad里建立4Button,Camera调用相机、Library调用图片库、flashlight打开闪光灯、close关闭闪光灯
[java] view
plaincopy
//打开相机
-(void)addCarema
{
//判断是否可以打开相机,模拟器此功能无法使用
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
UIImagePickerController * picker = [[UIImagePickerController alloc]init];
picker.delegate = self;
picker.allowsEditing = YES; //是否可编辑
//摄像头
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentModalViewController:picker animated:YES];
[picker release];
}else{
//如果没有提示用户
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"你没有摄像头" delegate:nil cancelButtonTitle:@"Drat!" otherButtonTitles:nil];
[alert show];
}
}
打开相机后,然后需要调用UIImagePickerControllerDelegate里的方法,拍摄完成后执行的方法和点击Cancel之后执行的方法:
[java] view
plaincopy
//拍摄完成后要执行的方法
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
//得到图片
UIImage * image = [info objectForKey:UIImagePickerControllerOriginalImage];
//图片存入相册
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
[self dismissModalViewControllerAnimated:YES];
}
//点击Cancel按钮后执行方法
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[self dismissModalViewControllerAnimated:YES];
}
调用相机照片和保存到图片库已经完成。
接着介绍打开照片库:
[java] view
plaincopy
-(void)openPicLibrary
{
//相册是可以用模拟器打开的
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
UIImagePickerController * picker = [[UIImagePickerController alloc]init];
picker.delegate = self;
picker.allowsEditing = YES;//是否可以编辑
//打开相册选择照片
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:picker animated:YES];
[picker release];
}else{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"你没有摄像头" delegate:nil cancelButtonTitle:@"Drat!" otherButtonTitles:nil];
[alert show];
}
}
//选中图片进入的代理方法
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
[self dismissModalViewControllerAnimated:YES];
}
调用闪光灯的代码
[java] view
plaincopy
-(void)openFlashlight
{
AVCaptureDevice * device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if (device.torchMode == AVCaptureTorchModeOff) {
//Create an AV session
AVCaptureSession * session = [[AVCaptureSession alloc]init];
// Create device input and add to current session
AVCaptureDeviceInput * input = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil];
[session addInput:input];
// Create video output and add to current session
AVCaptureVideoDataOutput * output = [[AVCaptureVideoDataOutput alloc]init];
[session addOutput:output];
// Start session configuration
[session beginConfiguration];
[device lockForConfiguration:nil];
// Set torch to on
[device setTorchMode:AVCaptureTorchModeOn];
[device unlockForConfiguration];
[session commitConfiguration];
// Start the session
[session startRunning];
// Keep the session around
[self setAVSession:self.AVSession];
[output release];
}
}
-(void)closeFlashlight
{
[self.AVSession stopRunning];
二、查看符合的媒体类型,这个时候我们调用availableMediaTypeForSourceType:判断
在调用UIImagePickerController时我们需要加入他的两个代理方法:
UINavigationControllerDelegate和UIImagePickerControllerDelegate,在调用摄像头的时候还可以调闪光灯,一会代码里有。
要调用闪光灯需要先建一个AVCaptureSession类的实例对象:
[java] view
plaincopy
#import <UIKit/UIKit.h>
//调用闪光灯调用框架
#import <AVFoundation/AVFoundation.h>
@interface CameraViewController : UIViewController<UINavigationControllerDelegate, UIImagePickerControllerDelegate>
{
AVCaptureSession * _AVSession;//调用闪光灯的时候创建的类
}
@property(nonatomic,retain)AVCaptureSession * AVSession;
@end
在.m的- (void)viewDidLoad里建立4Button,Camera调用相机、Library调用图片库、flashlight打开闪光灯、close关闭闪光灯
[java] view
plaincopy
//打开相机
-(void)addCarema
{
//判断是否可以打开相机,模拟器此功能无法使用
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
UIImagePickerController * picker = [[UIImagePickerController alloc]init];
picker.delegate = self;
picker.allowsEditing = YES; //是否可编辑
//摄像头
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentModalViewController:picker animated:YES];
[picker release];
}else{
//如果没有提示用户
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"你没有摄像头" delegate:nil cancelButtonTitle:@"Drat!" otherButtonTitles:nil];
[alert show];
}
}
打开相机后,然后需要调用UIImagePickerControllerDelegate里的方法,拍摄完成后执行的方法和点击Cancel之后执行的方法:
[java] view
plaincopy
//拍摄完成后要执行的方法
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
//得到图片
UIImage * image = [info objectForKey:UIImagePickerControllerOriginalImage];
//图片存入相册
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
[self dismissModalViewControllerAnimated:YES];
}
//点击Cancel按钮后执行方法
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[self dismissModalViewControllerAnimated:YES];
}
调用相机照片和保存到图片库已经完成。
接着介绍打开照片库:
[java] view
plaincopy
-(void)openPicLibrary
{
//相册是可以用模拟器打开的
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
UIImagePickerController * picker = [[UIImagePickerController alloc]init];
picker.delegate = self;
picker.allowsEditing = YES;//是否可以编辑
//打开相册选择照片
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:picker animated:YES];
[picker release];
}else{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"你没有摄像头" delegate:nil cancelButtonTitle:@"Drat!" otherButtonTitles:nil];
[alert show];
}
}
//选中图片进入的代理方法
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
[self dismissModalViewControllerAnimated:YES];
}
调用闪光灯的代码
[java] view
plaincopy
-(void)openFlashlight
{
AVCaptureDevice * device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if (device.torchMode == AVCaptureTorchModeOff) {
//Create an AV session
AVCaptureSession * session = [[AVCaptureSession alloc]init];
// Create device input and add to current session
AVCaptureDeviceInput * input = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil];
[session addInput:input];
// Create video output and add to current session
AVCaptureVideoDataOutput * output = [[AVCaptureVideoDataOutput alloc]init];
[session addOutput:output];
// Start session configuration
[session beginConfiguration];
[device lockForConfiguration:nil];
// Set torch to on
[device setTorchMode:AVCaptureTorchModeOn];
[device unlockForConfiguration];
[session commitConfiguration];
// Start the session
[session startRunning];
// Keep the session around
[self setAVSession:self.AVSession];
[output release];
}
}
-(void)closeFlashlight
{
[self.AVSession stopRunning];
相关文章推荐
- IOS Masonry自动布局
- OC属性中setter和getter的实现
- ios机器上,应用列表的获得
- IOS NSBundle
- ios时间时区处理
- NSPredicate
- 读IOS进阶笔记。
- IOS中数据的存储方式总结 (待续。。。。。)
- iOS字体
- [IOS]本地通知
- 关于iOS中几种延时方式
- iOS 编程之 使用 Xcode6配置.pch文件
- iOS:KVO的概述与使用
- iOS 身份证号校验
- 关于 initWithNibName 和 loadNibNamed 的区别和联系
- iOS 9 分屏多任务
- IOS MD5 + Base64加密
- ios激情详解之通知
- iOS学习笔记(二)
- iOS扫描二维码的实现