iOS_調用攝像頭拍照

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

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

發佈了29 篇原創文章 · 獲贊 8 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章