IOS 调用系统相机(备用)

IOS系统照相机的调用

其他代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
IOS开发调用系统相机和打开闪光灯
 
    今天给大家分享一下如何调用iphone的拍照功能和打开闪光灯,有些代码我也不太理解,很多是在网上借鉴其他人的。IOS有两种的拍照和视频的方式:1.直接使用UIImagePickerController,这个类提供了一个简单便捷的拍照与选择图片库里图片的功能。2.另一种是通过AVFoundation.framework框架完全自定义拍照的界面和选择图片库界面。我只做了第一种,就先给大家介绍第一种做法:
一、首先调用接口前,我们需要先判断当前设备是否支持UIImagePickerController,用isSourceTypeAvailable:来判断是否可用
二、查看符合的媒体类型,这个时候我们调用availableMediaTypeForSourceType:判断
在调用UIImagePickerController时我们需要加入他的两个代理方法:
UINavigationControllerDelegate和UIImagePickerControllerDelegate,在调用摄像头的时候还可以调闪光灯,一会代码里有。
要调用闪光灯需要先建一个AVCaptureSession类的实例对象:
//
//  Created by Mason on 13-1-23.
//  Copyright (c) 2013年 Mason. All rights reserved.
//
 
#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关闭闪光灯
//打开相机
-(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之后执行的方法:
//拍摄完成后要执行的方法
-(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];
}
调用相机照片和保存到图片库已经完成。
接着介绍打开照片库:
-(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];
}
调用闪光灯的代码,由于我也不是很理解,所以没法加注释,但是已经亲测可用,但是调闪光灯时有一个算是bug吧,闪光灯会闲一下,然后再一直亮
-(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];
     [self.AVSession release];
}
描述:直接使用UIImagePickerController调用iphone的拍照功能和打开闪光灯
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值