iOS_呼叫攝像頭拍照

NO IMAGE

本文修改了來自網路上舊程式碼中一些不再被推薦使用的方法。

廢話不多說,直接上程式碼!以下程式碼均為.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