IOS Call System Camera Photo album feature

Source: Internet
Author: User

Write a button to copy the following code directly

Views must inherit

<UIImagePickerControllerDelegate,UIPopoverControllerDelegate,UIActionSheetDelegate> Agent

Taking photos

-(void) Showactionsheet

{

Uiactionsheet *sheet;

if ([Uiimagepickercontroller Issourcetypeavailable:uiimagepickercontrollersourcetypecamera])

{

sheet = [[Uiactionsheet alloc] initwithtitle:@ "select" Delegate:self cancelbuttontitle:nil destructivebuttontitle:@ "Cancel" otherbuttontitles:@ "Photo", @ "select from album", Nil];

}

else {

sheet = [[Uiactionsheet alloc] initwithtitle:@ "select" Delegate:self cancelbuttontitle:nil destructivebuttontitle:@ "Cancel" Otherbuttontitles:@ "Select from album", Nil];

}

Sheet.tag = 255;

[Sheet ShowInView:self.view];

}

Upload Avatar

-(void) Actionsheet: (Uiactionsheet *) Actionsheet Clickedbuttonatindex: (Nsinteger) Buttonindex

{

if (Actionsheet.tag = = 255) {

Nsuinteger sourcetype = 0;

Determine if the camera is supported

if ([Uiimagepickercontroller Issourcetypeavailable:uiimagepickercontrollersourcetypecamera]) {

Switch (buttonindex) {

Case 0:

Cancel

Return

Case 1:

Camera

SourceType = Uiimagepickercontrollersourcetypecamera;

Break

Case 2:

Album

SourceType = uiimagepickercontrollersourcetypephotolibrary;

Break

}

}

else {

if (Buttonindex = = 0) {

Return

} else {

SourceType = Uiimagepickercontrollersourcetypesavedphotosalbum;

}

}

Jump to the camera or album page

Uiimagepickercontroller *imagepickercontroller = [[Uiimagepickercontroller alloc] init];

Imagepickercontroller.delegate = self;

imagepickercontroller.allowsediting = YES;

Imagepickercontroller.sourcetype = sourcetype;

[Self Presentviewcontroller:imagepickercontroller Animated:yes completion:^{}];

[Imagepickercontroller release];

}

}

#pragma mark-image Picker Delegte

-(void) Imagepickercontroller: (Uiimagepickercontroller *) Picker Didfinishpickingmediawithinfo: (NSDictionary *) info

{

[Picker Dismissviewcontrolleranimated:yes completion:^{}];

UIImage *image = [info objectforkey:uiimagepickercontrolleroriginalimage];

/* Here info has six values

* UIIMAGEPICKERCONTROLLERMEDIATYPE; An nsstring uttypeimage)

* UIIMAGEPICKERCONTROLLERORIGINALIMAGE; A UIImage original image

* UIIMAGEPICKERCONTROLLEREDITEDIMAGE; A UIImage image after cropping

* UIIMAGEPICKERCONTROLLERCROPRECT; An Nsvalue (CGRect)

* UIIMAGEPICKERCONTROLLERMEDIAURL; An Nsurl

* Uiimagepickercontrollerreferenceurl//An nsurl that references a asset in the Assetslibrary framework

* Uiimagepickercontrollermediametadata//An nsdictionary containing metadata from a captured photo

*/

Save picture to local, see below

[Self saveimage:image withname:@ "currentimage.png"];

_fullpath = [[Nshomedirectory () stringbyappendingpathcomponent:@ "Documents"] stringbyappendingpathcomponent:@ " Currentimage.png "];

UIImage *savedimage = [[UIImage alloc] initwithcontentsoffile:_fullpath];

[_imagebutton setbackgroundimage:savedimage forstate:0];

}

-(void) Imagepickercontrollerdidcancel: (Uiimagepickercontroller *) picker

{

[Self Dismissviewcontrolleranimated:yes completion:^{}];

}

-(void) SaveImage: (UIImage *) currentimage withname: (NSString *) imageName

{

NSData *imagedata = uiimagejpegrepresentation (currentimage, 0.5);

Get the Sandbox catalog

NSString *fullpath = [[Nshomedirectory () stringbyappendingpathcomponent:@ "Documents"] Stringbyappendingpathcomponent:imagename];

Write a picture to a file

[ImageData Writetofile:fullpath Atomically:no];

}

IOS Call System Camera Photo album feature

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.