iOS_调用摄像头拍照

时间:2022-09-20 13:31:17

本文修改了来自网络上旧代码中一些不再被推荐使用的方法。

废话不多说,直接上代码!以下代码均为.m源文件

#import "ViewController.h"

@interface ViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
[superviewDidLoad];

UIButton *takePhoto = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];
[takePhoto setTitle:@"录像"forState:UIControlStateNormal];
[takePhoto addTarget:selfaction:@selector(takePhoto)forControlEvents:UIControlEventTouchUpInside];
takePhoto.frame = CGRectMake(50,100,100,30);
[self.view addSubview:takePhoto];
}

- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
}

-(void)takePhoto
{
UIImagePickerControllerSourceType sourceType=UIImagePickerControllerSourceTypeCamera;
if (![UIImagePickerControllerisSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
}
UIImagePickerController * picker = [[UIImagePickerControlleralloc]init];
picker.delegate=self;
picker.allowsEditing=YES;
picker.sourceType=sourceType;
[selfpresentViewController:pickeranimated:YEScompletion:nil];
}

-(void)saveImage:(UIImage*)image
{
NSLog(@"保存");
}

-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[picker dismissViewControllerAnimated:YEScompletion:nil];
UIImage * image=[info objectForKey:UIImagePickerControllerEditedImage];
[selfperformSelector:@selector(saveImage:)withObject:imageafterDelay:0.5];
}

-(void)imagePickerControllerDIdCancel:(UIImagePickerController*)picker
{
[picker dismissViewControllerAnimated:YEScompletion:nil];
}


真机亲测可用,能进行调焦距。


参考链接:http://mobile.51cto.com/iphone-278579.htm