iOS 调用系统相册,调用系统照相机,调用系统相册,并且对照片进行剪裁

来源:互联网 发布:电路图制作软件下载 编辑:程序博客网 时间:2024/05/22 00:53

今天要修改上传头像,化验单的功能。上传头像可以从相册中选择照片上传,也可以拍照上传。从相册上传的时候,可以对照片进行剪裁。

调用系统的相册,或者照相机,需要用到UIImagePickerController,使用UIImagePickerController 需要有两个代理,分别是UIImagePickerControllerDelegate,和

UINavigationControllerDelegate。

UIImagePickerControllerSourceType 
是用来设置调用UIImagePickerController 的类型了,若果sourceType = UIImagePickerControllerSourceTypeCamera;是调用相机。如果imagePickerController.sourceType=UIImagePickerControllerSourceTypePhotoLibrary; 是调用系统相册。
 imagePickerController.allowsEditing=YES;//表示可以编辑对照片进行编辑,会多出来一个正方形的框。个人认为只针对照片有用。


#pragma UIActionSheetDelegateMethod-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{    if(buttonIndex==0)    {        NSLog(@"拍照");        UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;                picker1 = [[UIImagePickerController alloc] init];//初始化        picker1.delegate = self;                picker1.allowsEditing = YES;//设置可编辑        picker1.sourceType = sourceType;        [self presentViewController:picker1 animated:YES completion:nil];    }    else if(buttonIndex==1)    {        NSLog(@"从手机相册选择");        imagePickerController=[[UIImagePickerController alloc]init];        imagePickerController.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;        imagePickerController.delegate = self;        imagePickerController.allowsEditing=YES;        [self presentViewController:imagePickerController animated:YES completion:^{                    }];    }    else if(buttonIndex==2)    {        NSLog(@"取消");    }} 

这里还有一个回调的方法-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info,见名知意。

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{            if(picker==picker1)    {        UIImage *image=[info objectForKey:@"UIImagePickerControllerOriginalImage"];//picker1 是拍照,要把拍下的照片保存到相册里面。        UIImageWriteToSavedPhotosAlbum(image, self, nil, nil);        UIImage* image1 = [info objectForKey: @"UIImagePickerControllerEditedImage"];//把编辑后的图片发送给Server        imageData=UIImagePNGRepresentation(image1);                NSString *img_data_str=[imageData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];        [self httpSetUserPhotoRequest:img_data_str];    }        else if(picker==imagePickerController)    {                UIImage* image1 = [info objectForKey: @"UIImagePickerControllerEditedImage"];        imageData=UIImagePNGRepresentation(image1);                NSString *img_data_str=[imageData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];        [self httpSetUserPhotoRequest:img_data_str];           }        [self dismissViewControllerAnimated:YES completion:NULL]; }


0 0