// 生成一个UIImagePickerController
- (UIImagePickerController *)pickerController
{
UIImagePickerController *pickerController = [[UIImagePickerController alloc] init];
// 设置导航栏的背景⾊
pickerController.navigationBar.tintColor = [UIColor whiteColor];
pickerController.navigationBar.translucent = NO;
[pickerController.navigationBar setBackgroundImage:[UIImage imageWithColor:ICAN_BLACK_COLOR] forBarMetrics:UIBarMetricsDefault];
// 设置导航栏字体颜色
NSDictionary *dict = @{ NSForegroundColorAttributeName : [UIColor whiteColor], NSFontAttributeName : ICAN_FONT(18)};
pickerController.navigationBar.titleTextAttributes = dict;
pickerController.navigationBar.barStyle = UIBarStyleDefault;
// 设置代理
pickerController.delegate = self;
return pickerController;
}
#pragma mark - UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
UIImage *yourImage = image;
float compressionQuality = 0.6;//60 percent compression
CGRect rect = CGRectMake(0.0, 0.0, 256, 256);
UIGraphicsBeginImageContext(rect.size);
[yourImage drawInRect:rect];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
NSData *imageData = UIImageJPEGRepresentation(img, compressionQuality);
// 上传头像
}
/*If Don't pick any image*/
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
[picker dismissViewControllerAnimated:YES completion:NULL];
}
#pragma mark - 照相相关,点击照片或者照相
/*Open Camera*/
- (void)takePhoto {
UIImagePickerController *picker = [self pickerController];
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker animated:YES completion:nil];
}
/*Select Photo*/
- (void)selectPhoto {
UIImagePickerController *picker = [self pickerController];
// picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
picker.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[self presentViewController:picker animated:YES completion:nil];
}
// 修改UIImagePickerController导航栏文字为中文
// 在Info.plist中修改Key:Localization native development region,value = China即可