首页 > 代码库 > 调去系统照相机或者从本地相册获取图片.
调去系统照相机或者从本地相册获取图片.
1.首先要遵守三个协议
UIActionSheetDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate
2.主要代码
//选择头像 - (IBAction)handleSelectImage:(id)sender { self.myActionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"打开照相机",@"从相册中获取", nil]; [self.myActionSheet showInView:self.view]; } #pragma mark - UIActionSheetDelegate - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{ if (buttonIndex == self.myActionSheet.cancelButtonIndex) { NSLog(@"你取消了"); } switch (buttonIndex) { case 0: [self takePhoto]; break; case 1: [self localPhoto]; break; default: break; } } //照相 - (void)takePhoto{ UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera; if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]) { UIImagePickerController *picker = [[UIImagePickerController alloc] init]; picker.delegate = self; //设置拍照后的图片可被编辑 picker.allowsEditing = YES; picker.sourceType = sourceType; [self presentViewController:picker animated:YES completion:nil]; }else { NSLog(@"模拟其中无法打开照相机,请在真机中使用"); } } //选取本地图片 - (void)localPhoto{ UIImagePickerController *picker = [[UIImagePickerController alloc] init]; picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; picker.delegate = self; picker.allowsEditing = YES; [self presentViewController:picker animated:YES completion:nil]; } //当选择一张图片后进入这里 -(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { NSString *type = [info objectForKey:UIImagePickerControllerMediaType]; //当选择的类型是图片 if ([type isEqualToString:@"public.image"]) { //先把图片转成NSData UIImage* image = [info objectForKey:@"UIImagePickerControllerOriginalImage"]; NSData *data; if (UIImagePNGRepresentation(image) == nil) { data = http://www.mamicode.com/UIImageJPEGRepresentation(image, 1.0);><pre name="code" class="objc">- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker { NSLog(@"您取消了选择图片"); [picker dismissViewControllerAnimated:YES completion:nil]; }调去系统照相机或者从本地相册获取图片.
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。