//头像点击事件
-(void)photoViewBtn{
UIActionSheet* sheet = [[UIActionSheet alloc
]initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"从手机相册里选择", @"拍照", nil];
sheet.tag = 1001;
[sheet showInView:self.view];
}
#pragma mark - UIActionSheetDelegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
//头像
if (actionSheet.tag == 1001) {
if (0 == buttonIndex) {
[self LocalPhoto];
} else if (1 == buttonIndex) {
[self takePhoto];
}
}
}
//打开本地相册
-(void)LocalPhoto
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
picker.delegate = self;
//设置选择后的图片可被编辑
picker.allowsEditing = YES;
[self presentViewController:picker animated:YES completion:nil];
}
//开始拍照
-(void)takePhoto
{
UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
if ([UIImagePickerController isSourceTypeAvailable:sourceType])
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.sourceType = sourceType;
picker.delegate = self;
//设置拍照后的图片可被编辑
picker.allowsEditing = YES;
[self.view.window.rootViewController presentViewController:picker animated:YES completion:nil];
}else{
UIAlertView *al = [[UIAlertView alloc] initWithTitle:nil message:@"模拟器中无法打开照相机,请在真机中使用" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确认", nil];
[al show];
}
}
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
for (UINavigationItem *item in navigationController.navigationBar.subviews) {
if ([item isKindOfClass:[UIButton class]]&&([item.title isEqualToString:@"取消"]||[item.title isEqualToString:@"Cancel"]))
{
UIButton *button = (UIButton *)item;
[button setTitle:@"取消" forState:UIControlStateNormal];
[button setTintColor:[UIColor blackColor]];
}
}
}
//当选择一张图片后进入这里
-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSString *type = [info objectForKey:UIImagePickerControllerMediaType];
//当选择的类型是图片
if ([type isEqualToString:@"public.image"])
{
// 2.取得的图片
UIImage *image = info[UIImagePickerControllerOriginalImage];
NSData *imageData = UIImageJPEGRepresentation(image, 1.0);
UIImage *img = [UIImage imageWithData:imageData];
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 90, 90)];
imgView.image = img;
NSData *imgViewData = [self imageWithImage:imgView.image scaledToSize:CGSizeMake(90, 90)];
UIImage *suolue = [UIImage imageWithData:imgViewData]
//上传头像接口在这里调用
[picker dismissViewControllerAnimated:YES completion:nil];
}
}
//将图片缩略到指定大小
- (NSData *)imageWithImage:(UIImage*)image
scaledToSize:(CGSize)newSize;
{
UIGraphicsBeginImageContext(newSize);
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return UIImageJPEGRepresentation(newImage, 0.5);
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[picker dismissViewControllerAnimated:YES completion:nil];
}