iOS開發中訪問相簿攝像像頭

來源:互聯網
上載者:User

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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.