iOS 上传图片

2018-11-02  本文已影响0人  壮士你怎么了
-(void)updateUserInfo99999
{
    NSString *gender = [[NSString alloc]init];
    if ([self.label_sub_xb.text isEqualToString:@"男"]) {
        gender = @"0";
    }else{
        gender = @"1";
    }
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer.timeoutInterval = 20;
    [manager.requestSerializer setValue:@"multipart/form-data" forHTTPHeaderField:@"Content-Type"];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain", @"multipart/form-data", @"application/json", @"text/html", @"image/jpeg", @"image/jpg", @"image/png", @"application/octet-stream", @"text/json", nil];
    
    NSDictionary *parameterDict = @{@"username":self.label_sub_nc.text,
                               @"phone":@"",
                               @"nickname":self.label_sub_nc.text,
                               @"gender":gender,
                               @"birth":self.label_sub_cs.text,
                               @"loginId":@"123456",
                               @"signature":self.label_sub_qm.text
                               };
    NSLog(@"dicParam = %@",parameterDict);
    UIImage *image = self.imageView_tx.image;
    NSMutableArray *arr_image = [[NSMutableArray alloc]init];
    [arr_image addObject:image];
    NSLog(@"image = %@",image);
    NSLog(@"arr_image = %@",arr_image);
    [manager POST:[NSString stringWithFormat:@"http://192.168.0.13:808--------"] parameters:parameterDict constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        
        for (NSUInteger i = 0; i < arr_image.count; i++) {
            // 图片经过等比压缩后得到的二进制文件
            NSData *imageData = UIImageJPEGRepresentation(arr_image[i], 1.f);
            // 默认图片的文件名, 若fileNames为nil就使用
            
            NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
            formatter.dateFormat = @"yyyyMMddHHmmss";
            NSString *str = [formatter stringFromDate:[NSDate date]];
            NSString *imageFileName = [NSString stringWithFormat:@"%@%ld.%@",str,i,@"jpg"];
            NSLog(@"imageData = %@",imageData);
            
            [formData appendPartWithFileData:imageData
                                        name:@"file"
                                    fileName:imageFileName
                                    mimeType:[NSString stringWithFormat:@"image/%@",@"jpg"]];
        }
        
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        
        //上传进度
        dispatch_sync(dispatch_get_main_queue(), ^{
            NSLog(@"progress is %@",uploadProgress);
        });
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"responseObject = %@",responseObject);
        NSLog(@"message = %@",[responseObject valueForKey:@"message"]);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        NSLog(@"error = %@",error);
        
    }];
}
上一篇下一篇

猜你喜欢

热点阅读