博客> 3.0.4版本AFNetworking上传头像(附下载地址)
3.0.4版本AFNetworking上传头像(附下载地址)
2017-09-23 01:47 评论:0 阅读:210 飞翔信念
3.0.4版本AFNetworking上传头像(附下载地址)

最近用到头像上传功能,因为之前用的AFNetworking是从别的项目中拖过来的,也不知道版本,网上搜了很多,感觉自己的AFNetworking很奇怪,然后果断换为3.0.4版本,用到的调用相册参考的这篇博客:http://blog.cocoachina.com/article/33116
AFNetworking各个版本下载地址:https://github.com/AFNetworking/AFNetworking/tags 下面直接上获取相册及头像上传的代码

mySheet=[[UIActionSheet alloc]initWithTitle:@"选择拍照方式" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"拍照" otherButtonTitles:@"相册", nil];
                    [mySheet showInView:self.view];

#pragma mark  acionSheet的代理方法
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSInteger sourceType=0;

    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        switch (buttonIndex) {
            case 0:
            {
                //相机
                sourceType=UIImagePickerControllerSourceTypeCamera;
                            }
                break;
            case 1:
            {
                //相册
                sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
            }
                break;
            case 2:
            {
                //取消
                return;

            }
                break;
            default:
                break;
        }
    } else {
        if (buttonIndex==2) {
            //取消
            return;
        } else {
            //相册
            sourceType=UIImagePickerControllerSourceTypeSavedPhotosAlbum;
        }
    }
    // 跳转到相机或相册页面
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];

    imagePickerController.delegate = self;

    imagePickerController.allowsEditing = YES;

    imagePickerController.sourceType = sourceType;

    [self presentViewController:imagePickerController animated:YES completion:^{}];

}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [picker dismissViewControllerAnimated:YES completion:^{}];

    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];

    [self saveImage:image withName:@"currentImage.png"];

    NSString *fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"currentImage.png"];
    isChange=YES;

    //1.创建管理者对象
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    //2.上传文件
    NSDictionary *dict = @{@"uid":[[NSUserDefaults standardUserDefaults] objectForKey:@"uid"],@"head_pic":@"currentImage",@"token":[[NSUserDefaults standardUserDefaults] objectForKey:@"token"]};

    NSString *urlString = [NSString stringWithFormat:@"%@%@",HTTP_URL,POST_USER_UPDATE];
    [manager POST:urlString parameters:dict constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {

        [formData appendPartWithFileURL:[NSURL fileURLWithPath:fullPath] name:@"head_pic" fileName:@"currentImage.png" mimeType:@"image/png" error:nil];
    } progress:^(NSProgress * _Nonnull uploadProgress) {

        //打印下上传进度
        NSLog(@"%lf",1.0 *uploadProgress.completedUnitCount / uploadProgress.totalUnitCount);
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {

        //请求成功
        NSLog(@"请求成功:%@",responseObject);
        NSLog(@"请求地址:%@",urlString);
        NSLog(@"请求参数:%@",dict);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

        //请求失败
        NSLog(@"请求失败:%@",error);
    }];

}
-(void) saveImage:(UIImage *)currentImage withName:(NSString *)imageName
{

    NSData * imageData = UIImageJPEGRepresentation(currentImage, 0.5);
    // 获取沙盒目录
    NSString * fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:imageName];

    // 将图片写入文件
    [imageData writeToFile:fullPath atomically:NO];
}
收藏
0
sina weixin mail 回到顶部