afnetwork图片上传的时候出错,出现错误

2015-11-09 15:47:59.086 videoPro[3207:132795] *** Assertion failure in -[AFStreamingMultipartFormData appendPartWithHeaders:body:], /Users/zhanglingxiang/Documents/videoPro/AFNetworking/AFURLRequestSerialization.m:793

2015-11-09 15:48:01.655 videoPro[3207:132795] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid parameter not satisfying: body'

 

代码:

 NSData *imageData = UIImageJPEGRepresentation(image,0.1f);
    
    //转码成UTF-8  否则可能会出现错误
    NSString *URLTmp = [NSString stringWithFormat:@"%@/photoUpload",SERVER];
    
    NSString *URLTmps = [URLTmp stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    URLTmp = URLTmps;
    
    NSDictionary* dic = [NSDictionary dictionaryWithObject:self.userName forKey:@"user"];
    
    NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:URLTmp parameters:dic constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        
        [formData appendPartWithFileData:imageData name:@"headerImg" fileName:@"123.png" mimeType:@"image/png"];
        
        
    } error:nil];
    
    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    
    NSProgress *progress = nil;

    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithStreamedRequest:request progress:&progress completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
        
        if (error) {
            
            NSLog(@"Error: %@", error);
            
        } else {
            NSLog(@"%@",responseObject);
            NSDictionary* dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
            NSString* strHead = [NSString stringWithFormat:@"%@/%@",IMGSERVER,dic[@"url"]];
//            [[NSUserDefaults standardUserDefaults] setObject: forKey:@"userHead"];
            [imageView sd_setImageWithURL:[NSURL URLWithString:strHead]];
            
            //头像上传成功
            MBProgressHUD* hud = [[MBProgressHUD alloc] initWithView:self.view];
            [self.view addSubview:hud];
            hud.mode = MBProgressHUDModeCustomView;
            hud.labelText = @"头像上传成功";
            [hud showAnimated:YES whileExecutingBlock:^{
                sleep(2);
            } completionBlock:^{
                [hud removeFromSuperview];
                return;
            }];

            
        }
    }];
    [uploadTask resume];

  

 

改了几种方法,都不行,最后发现是image没有值,也是醉了。。

 

 

posted on 2015-11-09 15:41  小丑鱼2012  阅读(2459)  评论(0编辑  收藏  举报