iOS头像编辑上传功能

作者: XH小子 | 来源:发表于2016-07-19 22:11 被阅读2675次

iOS的相册选择和拍照就自带有照片的编辑功能,在这里我就利用了这一功能对头像编辑做了一个简单的封装。

1.首先创建一个单列以及定义一个block来回调选择之后的照片。

@interface UpLoadUserpicTool ()<UIImagePickerControllerDelegate, UINavigationControllerDelegate>


@property (nonatomic, strong)UIViewController *viewController;

@property (nonatomic, copy)FinishSelectImageBlcok imageBlock;

@end
+ (UpLoadUserpicTool *)shareManager
{
    static UpLoadUserpicTool *managerInstance = nil;
    static dispatch_once_t token;
    dispatch_once(&token, ^{
        managerInstance = [[self alloc] init];
    });
    return managerInstance;
}

2.创建一个方法,选择上传照片的方式,以及初始化回调的block.这里用的是UIAlertController来实现照片来源的选择。然后关键的一点就是UIImagePickerController有一个属性allowsEditing(是否允许编辑),设置为YES。

- (void)selectUserpicSourceWithViewController:(UIViewController *)viewController FinishSelectImageBlcok:(FinishSelectImageBlcok)finishSelectImageBlock{
    
    if (viewController) {
        self.viewController = viewController;
    }
    if (finishSelectImageBlock) {
        self.imageBlock = finishSelectImageBlock;
    }
    UIAlertController *alertCol = [UIAlertController alertControllerWithTitle:@"请选择头像来源" message:@"" preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])///<检测该设备是否支持拍摄
        {
            //                    [Tools showAlertView:@"sorry, 该设备不支持拍摄"];///<显示提示不支持
            
            return;
        }
        UIImagePickerController* picker = [[UIImagePickerController alloc]init];///<图片选择控制器创建
        
        picker.sourceType = UIImagePickerControllerSourceTypeCamera;///<设置数据来源为拍照
        picker.allowsEditing = YES;
        picker.delegate = self;///<代理设置
        
        [self.viewController presentViewController:picker animated:YES completion:nil];///<推出视图控制器
        
        
    }];
     [alertCol addAction:action];
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"相册" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        
        UIImagePickerController* picker = [[UIImagePickerController alloc]init];///<图片选择控制器创建
        
        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;///<设置数据来源为相册
        //允许选择照片之后可编辑
        picker.allowsEditing = YES;
        picker.delegate = self;///<代理设置
        
        [viewController presentViewController:picker animated:YES completion:nil];///<推出视图控制器
        
    }];
    [alertCol addAction:action2];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
       
    
        
    }];
    [alertCol addAction:cancelAction];
   
    [viewController presentViewController:alertCol animated:YES completion:^{
        
        
    }];
  
}

3.编辑完成之后照片的获取,这里info中获取的照片要选择UIImagePickerControllerEditedImage类型的,这个是编辑之后的照片。

#pragma mark - 相册/相机回调  显示所有的照片,或者拍照选取的照片
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *image = nil;
    //获取编辑之后的图片
    image = [info objectForKey:UIImagePickerControllerEditedImage];
    if (self.imageBlock) {
        
        self.imageBlock(image);
    }
    
    [self.viewController dismissViewControllerAnimated:YES completion:nil];
    
}


//  取消选择 返回当前试图
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    
    [picker dismissViewControllerAnimated:YES completion:nil];
    
}

利用自带的实现头像编辑就是这么简单,但是如果你要改变编辑框的形状或者是大小的话,这种方法是不可以的,需要自己去自定义了。

最后....

为了偷懒将dome和UIAlertController写在了一起。
这是关于AlertCntroller文章:
http://www.jianshu.com/p/b89c3e96aba6
dome下载地址
[下载链接]https://git.oschina.net/ioszxh/iOS-UIAlertController

相关文章

网友评论

    本文标题:iOS头像编辑上传功能

    本文链接:https://www.haomeiwen.com/subject/kcxvjttx.html