IOS开发调用系统相机和打开闪光灯

IOS开发调用系统相机和打开闪光灯
 
   今天给大家分享一下如何调用iphone的拍照功能和打开闪光灯,有些代码我也不太理解,很多是在网上借鉴其他人的。IOS有两种的拍照和视频的方式:1.直接使用UIImagePickerController,这个类提供了一个简单便捷的拍照与选择图片库里图片的功能。2.另一种是通过AVFoundation.framework框架完全自定义拍照的界面和选择图片库界面。我只做了第一种,就先给大家介绍第一种做法:
一、首先调用接口前,我们需要先判断当前设备是否支持UIImagePickerController,用isSourceTypeAvailable:来判断是否可用
二、查看符合的媒体类型,这个时候我们调用availableMediaTypeForSourceType:判断
在调用UIImagePickerController时我们需要加入他的两个代理方法:
UINavigationControllerDelegate和UIImagePickerControllerDelegate,在调用摄像头的时候还可以调闪光灯,一会代码里有。

要调用闪光灯需要先建一个AVCaptureSession类的实例对象:

[java]  view plain copy
  1. #import <UIKit/UIKit.h>  
  2. //调用闪光灯调用框架  
  3. #import <AVFoundation/AVFoundation.h>  
  4.    
  5. @interface CameraViewController : UIViewController<UINavigationControllerDelegate, UIImagePickerControllerDelegate>  
  6. {  
  7.     AVCaptureSession * _AVSession;//调用闪光灯的时候创建的类  
  8. }  
  9.    
  10. @property(nonatomic,retain)AVCaptureSession * AVSession;  
  11.    
  12. @end  
在.m的- (void)viewDidLoad里建立4Button,Camera调用相机、Library调用图片库、flashlight打开闪光灯、close关闭闪光灯

[java]  view plain copy
  1. //打开相机  
  2. -(void)addCarema  
  3. {  
  4.     //判断是否可以打开相机,模拟器此功能无法使用  
  5.     if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {  
  6.            
  7.         UIImagePickerController * picker = [[UIImagePickerController alloc]init];  
  8.         picker.delegate = self;  
  9.         picker.allowsEditing = YES;  //是否可编辑  
  10.         //摄像头  
  11.         picker.sourceType = UIImagePickerControllerSourceTypeCamera;  
  12.         [self presentModalViewController:picker animated:YES];  
  13.         [picker release];  
  14.     }else{  
  15.         //如果没有提示用户  
  16.         UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"你没有摄像头" delegate:nil cancelButtonTitle:@"Drat!" otherButtonTitles:nil];  
  17.         [alert show];  
  18.     }  
  19. }  

打开相机后,然后需要调用UIImagePickerControllerDelegate里的方法,拍摄完成后执行的方法和点击Cancel之后执行的方法:

[java]  view plain copy
  1. //拍摄完成后要执行的方法  
  2. -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info  
  3. {  
  4.     //得到图片  
  5.     UIImage * image = [info objectForKey:UIImagePickerControllerOriginalImage];  
  6.     //图片存入相册  
  7.     UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);  
  8.     [self dismissModalViewControllerAnimated:YES];  
  9.        
  10. }  
  11. //点击Cancel按钮后执行方法  
  12. -(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker  
  13. {  
  14.     [self dismissModalViewControllerAnimated:YES];  
  15. }  


调用相机照片和保存到图片库已经完成。
接着介绍打开照片库:
[java]  view plain copy
  1. -(void)openPicLibrary  
  2. {  
  3.     //相册是可以用模拟器打开的  
  4.     if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {  
  5.         UIImagePickerController * picker = [[UIImagePickerController alloc]init];  
  6.         picker.delegate = self;  
  7.         picker.allowsEditing = YES;//是否可以编辑  
  8.    
  9.         //打开相册选择照片  
  10.         picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;  
  11.         [self presentModalViewController:picker  animated:YES];  
  12.         [picker release];  
  13.     }else{  
  14.         UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"你没有摄像头" delegate:nil cancelButtonTitle:@"Drat!" otherButtonTitles:nil];  
  15.         [alert show];  
  16.     }  
  17.        
  18. }  
  19.    
  20. //选中图片进入的代理方法  
  21. -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo  
  22. {  
  23.     [self dismissModalViewControllerAnimated:YES];  
  24. }  

调用闪光灯的代码

[java]  view plain copy
  1. -(void)openFlashlight  
  2. {  
  3.     AVCaptureDevice * device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];  
  4.     if (device.torchMode == AVCaptureTorchModeOff) {  
  5.         //Create an AV session  
  6.         AVCaptureSession * session = [[AVCaptureSession alloc]init];  
  7.            
  8.         // Create device input and add to current session  
  9.         AVCaptureDeviceInput * input = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil];  
  10.         [session addInput:input];  
  11.            
  12.         // Create video output and add to current session   
  13.         AVCaptureVideoDataOutput * output = [[AVCaptureVideoDataOutput alloc]init];  
  14.         [session addOutput:output];  
  15.            
  16.         // Start session configuration  
  17.         [session beginConfiguration];  
  18.         [device lockForConfiguration:nil];  
  19.            
  20.         // Set torch to on  
  21.         [device setTorchMode:AVCaptureTorchModeOn];  
  22.            
  23.         [device unlockForConfiguration];  
  24.         [session commitConfiguration];  
  25.            
  26.         // Start the session  
  27.         [session startRunning];  
  28.            
  29.         // Keep the session around  
  30.         [self setAVSession:self.AVSession];  
  31.            
  32.         [output release];  
  33.     }  
  34. }  
  35.    
  36. -(void)closeFlashlight  
  37. {  
  38.     [self.AVSession stopRunning];  
  39.     [self.AVSession release];  
  40. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值