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