Access to the album Camera in iOS development
Source Code http://download.csdn.net/download/jingjingxujiayou/7270479
In the APPDELEGATE.M file.
-(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 to self;} -(void) viewdidload{[Super viewdidload];//do any additional setup after loading the view. ----------1 loading pictures from the network 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]; Turn data into image uiimage* image = [UIImage Imagewithdata:data]; Show picture imageview1.image = Image; Convert the image to data nsdata* ImageData = uiimagejpegrepresentation (image, 1); NSLog (@ "%d", imagedata.length); Saved in the album, this can be viewed in the simulator of the album production. Uiimagewritetosavedphotosalbum (image, Self, @selector (image:didFinishSavingWithError:contextInfo:), NULL); ---------2-Album interview 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]; Access to the---------3 camera UIButton *buttoncamera = [UIButton buttonwithtype:uibuttontyperoundedrect]; Buttoncamera.tag = 200; Buttoncamera.frame = CGRectMake (50, 310, 80, 50); [Buttoncamera settitle:@ "Visit the camera" 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; Visit the type of album Type//uiimagepickercontrollersourcetypephotolibrary,//uiimagepickercontrollersourcetypecamera, = = = Access to the camera//uiimagepickercontrollersourcetypesavedphotosalbum ======= can only access the first column of the picture Imagepicker.sourcetype = UI Imagepickercontrollersourcetypephotolibrary; Display the [Self Presentviewcontroller:imagepicker Animated:yes completion:^{}] in a friction animation mode; if (Button.tag = =) {//uiimagepickercontrollercameradevicefront = = = Front Camera//uiimagepickercontrollercamera Devicerear = = = After camera BOOL iscamrma = [Uiimagepickercontroller Iscameradeviceavailable:uiimagepickercontrollercamerad Evicerear]; if (!iscamrma) {NSLog (@ "no camera"); Return }//Webcam imagepicker.sourcetype = Uiimagepickercontrollersourcetypecamera; Agree to edit imagepicker.allowsediting =yes; }}*/-(void) Look: (uibutton*) button{if (Button.tag = =) {Imagepicker = [[Uiimagepickercontroller alloc]init]; Imagepicker.delegate = self; Visit the type of album Type//uiimagepickercontrollersourcetypephotolibrary,//uiimagepickercontrollersourcetypecamera, = = = Access to the camera//uiimagepickercontrollersourcetypesavedphotosalbum ======= can only access the first column of the picture Imagepicker.sourcetype = UI Imagepickercontrollersourcetypephotolibrary; Display the [Self Presentviewcontroller:imagepicker Animated:yes completion:^{}] in a friction animation mode; }else {//Note camera access needs to be//uiimagepickercontrollercameradevicefront on the real machine = = = Front Camera//uiim Agepickercontrollercameradevicerear = = = After camera BOOL iscamrma = [Uiimagepickercontroller Iscameradeviceavailable:uiima Gepickercontrollercameradevicerear]; if (!iscamrma) {NSLog (@ "no camera"); Return }//Webcam imagepicker.sourcetype = Uiimagepickercontrollersourcetypecamera; Agree to edit imagepicker.allowsediting =yes; }}//---------2-album access #pragma mark-uiimagepickercontrollerdelegate//registration after selected call-(void) Imagepickercontroller: ( Uiimagepickercontroller *) Picker Didfinishpickingmediawithinfo: (nsdictionary *) info{// Uiimagepickercontrolleroriginalimage = = To take the original picture//uiimagepickercontrollereditedimage = = = Go To edit later picture uiimage* image = [i NFO Objectforkey:uiimagepickercontrollereditedimage]; Imageview1.image = image; NSLog (@ "info =%@", info); [Picker Dismissviewcontrolleranimated:yes completion:nil];} Cancel button's Click event-(void) Imagepickercontrollerdidcancel: (Uiimagepickercontroller *) picker{[picker Dismissviewcontrolleranimated:yes completion:null];} Save picture-(void) Image: (UIImage *) image didfinishsavingwitherror: (nserror *) error ContextInfo: (void *) contextinfo{ NSLog (@ "error =%@",Error);} -(void) didreceivememorywarning{[Super didreceivememorywarning]; Dispose of any resources the can be recreated.} @end
Access to the album Camera in iOS development