標籤:網路攝影機 imageview uiviewcontroller 資料 nsstring
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開發中訪問相簿攝像像頭