//添加代理方法
@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];
}
}
