一、跟以往最主要的区别在于,需要进行info.plist授权操作,具体如下:
1,相册。NSPhotoLibraryUsageDescription
1,导入头文件
#import "VPImageCropperViewController.h"
#import
#import
#import "UIImage+Resize.h"
#import "SVProgressHUD.h"
2,实现相应的委托UIImagePickerControllerDelegate,UIActionSheetDelegate,VPImageCropperDelegate,UINavigationControllerDelegate
3,添加相应的代码
#pragma mark --设置头像事件
//设置头像事件
-(void)setUserIcon
{
if ([[[UIDevice currentDevice] systemVersion] floatValue] > 8)
{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *takePhotoAction = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
[self goToImagePicker:YES];
//
}];
[alert addAction:takePhotoAction];
UIAlertAction *imageLibAction = [UIAlertAction actionWithTitle:@"从相册选取" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
[self goToImagePicker:NO];
}];
[alert addAction:imageLibAction];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action){
}];
[alert addAction:cancelAction];
// [alert.view setTintColor:UIColorFromRGB(0x50c878)];
[self presentViewController:alert animated:YES completion:nil];
}
else{
UIActionSheet* _actionSheet = [[UIActionSheet alloc]
initWithTitle:nil
delegate:self
cancelButtonTitle:@"取消"
destructiveButtonTitle:nil
otherButtonTitles:@"拍照", /*@"从相册选择",*/nil];
[_actionSheet showInView:self.view];
}
}
#pragma mark - 选相片代理
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
// 导入图片 or 拍照
if(buttonIndex == 0)
{
[self goToImagePicker:YES];
}
else if(buttonIndex == 1){
[self goToImagePicker:NO];
}
}
//yes:照相机 no:相册
-(void)goToImagePicker:(BOOL)camera
{
if (camera) {
self.isCamera=YES;
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
// [PublishVC goToCamera:self delegate:self];
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
UIImagePickerController *controller = [[UIImagePickerController alloc] init];
controller.sourceType = UIImagePickerControllerSourceTypeCamera;
//[controller setVideoQuality:UIImagePickerControllerQualityTypeLow];
NSMutableArray *mediaTypes = [[NSMutableArray alloc] init];
[mediaTypes addObject:(__bridge NSString *)kUTTypeImage];
controller.mediaTypes = mediaTypes;
controller.delegate = self;
[self presentViewController:controller
animated:YES
completion:^(void){
}];
}
}
}
else{
self.isCamera=NO;
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
{
UIImagePickerController *controller = [[UIImagePickerController alloc] init];
controller.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
NSMutableArray *mediaTypes = [[NSMutableArray alloc] init];
[mediaTypes addObject:(__bridge NSString *)kUTTypeImage];
controller.mediaTypes = mediaTypes;
controller.delegate = self;
[self presentViewController:controller
animated:YES
completion:^(void){
}];
}
}
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[picker dismissViewControllerAnimated:YES completion:^() {
UIImage *editedImage = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
if (self.isCamera) {
if (editedImage.size.width > SIZE_AVATAR) {
self.imageToSave = [editedImage resizedImage:CGSizeMake(SIZE_AVATAR, SIZE_AVATAR) interpolationQuality:kCGInterpolationDefault];
}
else
{
self.imageToSave = editedImage;
}
NSLog(@"1");
//提示框
[SVProgressHUD showSuccessWithStatus:@"正在上传图片"];
//调用设置头像接口和刷新UI
[self.indInfoTableView reloadData];
}
else{
int wantedPhotoWidth = kScreenWidth;
int clipHight = (kScreenHeight - wantedPhotoWidth) /2;
VPImageCropperViewController *imgEditorVC = [[VPImageCropperViewController alloc] initWithImage:editedImage cropFrame:CGRectMake(0, clipHight, wantedPhotoWidth, wantedPhotoWidth) limitScaleRatio:3.0];
imgEditorVC.delegate = self;
[self presentViewController:imgEditorVC animated:NO completion:nil];
NSLog(@"2");
}
}];
}
#pragma mark VPImageCropperDelegate
- (void)imageCropper:(VPImageCropperViewController *)cropperViewController didFinished:(UIImage *)editedImage {
self.imageToSave = nil;
NSLog(@"3");
//缩放图片
if (editedImage.size.width > SIZE_AVATAR) {
self.imageToSave = [editedImage resizedImage:CGSizeMake(SIZE_AVATAR, SIZE_AVATAR) interpolationQuality:kCGInterpolationDefault];
}
else
{
self.imageToSave = editedImage;
}
//提示框
[SVProgressHUD showSuccessWithStatus:@"正在上传图片"];
//调用设置头像接口和刷新UI
[cropperViewController dismissViewControllerAnimated:YES completion:^{
}];
[self.indInfoTableView reloadData];
}
- (void)imageCropperDidCancel:(VPImageCropperViewController *)cropperViewController
{
NSLog(@"4");
[cropperViewController dismissViewControllerAnimated:YES completion:^{
}];
}
此处的 self.imageToSave和self.isCamera分别是UIImage和Bool的两个属性值。
好了,至此就完成了基本的操作。