iOS開發中訪問相冊攝像像頭
阿新 • • 發佈:2017-05-22
created med res erro warn setup 上進 normal animate
dateViewController.h
dateViewController.m
iOS開發中訪問相冊攝像像頭
源代碼下載地址http://download.csdn.net/download/jingjingxujiayou/7270479
在AppDelegate.m文件裏
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after application launch. self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; self.window.rootViewController = [[dateViewController alloc]init]; return YES; }
dateViewController.h
#import <UIKit/UIKit.h> @interface dateViewController : UIViewController<UIImagePickerControllerDelegate,UINavigationBarDelegate> @property(nonatomic,retain)UIImageView* imageview1; @property(nonatomic,retain)UIImagePickerController* imagepicker; @end
dateViewController.m
// // dateViewController.m // datepick // // Created by 5_2 on 14-4-29. // Copyright (c) 2014年 Frountion. All rights reserved. // #import "dateViewController.h" @interface dateViewController () @end @implementation dateViewController @synthesize imageview1,imagepicker; - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. //----------1 從網絡載入圖片 imageview1 = [[UIImageView alloc]initWithFrame:CGRectMake(10, 50, 300, 200)]; imageview1.backgroundColor = [UIColor yellowColor]; [self.view addSubview:imageview1]; NSURL* url = [[NSURL alloc]initWithString:@"https://www.google.com.hk/images/srpr/logo11w.png"]; NSData* data = [NSData dataWithContentsOfURL:url]; //把data轉成image UIImage* image = [UIImage imageWithData:data]; //顯示圖片 imageview1.image = image; //把圖片轉化為數據 NSData* imagedata = UIImageJPEGRepresentation(image, 1); NSLog(@"%d",imagedata.length); //保存到相冊裏面,這個能夠到模擬器裏的相冊產查看的。 UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL); //---------2 相冊的訪問 UIButton *buttonphoto = [UIButton buttonWithType:UIButtonTypeRoundedRect]; buttonphoto.tag = 100; buttonphoto.frame = CGRectMake(50, 250, 80, 50); [buttonphoto setTitle:@"訪問相冊" forState:UIControlStateNormal]; [buttonphoto addTarget:self action:@selector(look:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:buttonphoto]; //---------3 攝像頭的訪問 UIButton *buttoncamera = [UIButton buttonWithType:UIButtonTypeRoundedRect]; buttoncamera.tag = 200; buttoncamera.frame = CGRectMake(50, 310, 80, 50); [buttoncamera setTitle:@"訪問攝像頭" forState:UIControlStateNormal]; [buttoncamera addTarget:self action:@selector(look:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:buttoncamera]; } /*-(void)look:(UIButton*)button { UIImagePickerController* imagepicker = [[UIImagePickerController alloc]init]; imagepicker.delegate = self; //訪問相冊類型的類型 //UIImagePickerControllerSourceTypePhotoLibrary, //UIImagePickerControllerSourceTypeCamera, ===== 訪問攝像頭 //UIImagePickerControllerSourceTypeSavedPhotosAlbum ======= 僅僅能訪問第一列的圖片 imagepicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; //以摩擦動畫的方式顯示 [self presentViewController:imagepicker animated:YES completion:^{ }]; if (button.tag == 200) { //UIImagePickerControllerCameraDeviceFront === 前攝像頭 //UIImagePickerControllerCameraDeviceRear === 後攝像頭 BOOL isCamrma = [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear]; if (!isCamrma) { NSLog(@"沒有攝像頭"); return; } //攝像頭 imagepicker.sourceType = UIImagePickerControllerSourceTypeCamera; //同意編輯 imagepicker.allowsEditing =YES; } }*/ -(void)look:(UIButton*)button { if (button.tag == 100) { imagepicker = [[UIImagePickerController alloc]init]; imagepicker.delegate = self; //訪問相冊類型的類型 //UIImagePickerControllerSourceTypePhotoLibrary, //UIImagePickerControllerSourceTypeCamera, ===== 訪問攝像頭 //UIImagePickerControllerSourceTypeSavedPhotosAlbum ======= 僅僅能訪問第一列的圖片 imagepicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; //以摩擦動畫的方式顯示 [self presentViewController:imagepicker animated:YES completion:^{ }]; }else { //註意攝像頭的訪問須要在真機上進行 //UIImagePickerControllerCameraDeviceFront === 前攝像頭 //UIImagePickerControllerCameraDeviceRear === 後攝像頭 BOOL isCamrma = [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear]; if (!isCamrma) { NSLog(@"沒有攝像頭"); return; } //攝像頭 imagepicker.sourceType = UIImagePickerControllerSourceTypeCamera; //同意編輯 imagepicker.allowsEditing =YES; } } //---------2 相冊的訪問 #pragma mark - UIImagePickerControllerDelegate //相冊選中之後調用 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { //UIImagePickerControllerOriginalImage === 取原始圖片 //UIImagePickerControllerEditedImage === 去編輯以後的圖片 UIImage* image = [info objectForKey:UIImagePickerControllerEditedImage]; imageview1.image = image; NSLog(@"info = %@",info); [picker dismissViewControllerAnimated:YES completion:nil]; } //取消按鈕的點擊事件 -(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker { [picker dismissViewControllerAnimated:YES completion:NULL]; } //將圖片保存 - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo { NSLog(@"error = %@",error); } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
iOS開發中訪問相冊攝像像頭