//添加代理方法
@interface MineViewController () <UITableViewDelegate, UITableViewDataSource, PayCellDelegate, UINavigationControllerDelegate, UIImagePickerControllerDelegate, UIActionSheetDelegate>
//定义消息框
UIActionSheet * act =[[UIActionSheet alloc]initWithTitle:@"请选择图片" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"选择相册",@"选择相机", nil];
//显示消息框
[act showInView:self.view];
//消息框代理实现
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
//定义图片选择器
UIImagePickerController * picker = [[UIImagePickerController alloc]init];
//判断
switch (buttonIndex) {
case 0:
//判断系统是否允许选择 相册
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
//图片选择是相册(图片来源自相册)
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
//设置代理
picker.delegate=self;
//模态显示界面
[self presentViewController:picker animated:YES completion:nil];
}
break;
case 1://判断系统是否允许选择 相机
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
//图片选择是相册(图片来源自相册)
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
//设置代理
picker.delegate=self;
//模态显示界面
[self presentViewController:picker animated:YES completion:nil];
}
else {
NSLog(@"不支持相机");
}
break;
default:
break;
}
}
//实现图片选择器代理
//参数:图片选择器 字典参数
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
//通过key值获取到图片
UIImage * image =info[UIImagePickerControllerOriginalImage];
NSLog(@"image=%@ info=%@",image, info);
//判断数据源类型
if (picker.sourceType == UIImagePickerControllerSourceTypePhotoLibrary) {
//设置图片背景
[userIconBtn setBackgroundImage:image forState:UIControlStateNormal];
NSUserDefaults * user = [NSUserDefaults standardUserDefaults];
NSData* imgData = UIImageJPEGRepresentation(image, 1);
isPhotoChoose = YES;
NSLog(@"第一次调用这个方法");
[user setObject:imgData forKey:@"saveIcon"];
[user synchronize];
[self dismissViewControllerAnimated:YES completion:nil];
}
if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
NSLog(@"在相机中选择图片");
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
//设置图片背景
[userIconBtn setBackgroundImage:image forState:UIControlStateNormal];
NSUserDefaults * user = [NSUserDefaults standardUserDefaults];
NSData* imgData = UIImageJPEGRepresentation(image, 1);
isPhotoChoose = YES;
NSLog(@"第一次调用这个方法");
[user setObject:imgData forKey:@"saveIcon"];
[user synchronize];
[self dismissViewControllerAnimated:YES completion:nil];
}
}