file类型图片上传

不需要考虑file转换问题,UIImageJPEGRepresentation(self.uploadImageView.allImages【i】,0.8)就是你想要的。

以下代码是我们用来传图的方式,其中name:@"images[]"换成你们后端程序对应的表单字段名

NSDictionary *param = @{@"type":self.typeId,

                            @"uid":self.userInfo[@"uid"],

                            @"project_id":self.projectId,

                            @"name":self.nameTextField.text,

                            @"info":self.infoTextView.text};

    NSLog(@"param:%@",param);

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

    [SVProgressHUD showWithStatus:@"正在上传图片" maskType:SVProgressHUDMaskTypeGradient];

    [manager POST:[NSString stringWithFormat:@"%@/projectLog/create",HOST_URL] parameters:param constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {

        for (int i = 0; i < self.uploadImageView.allImages.count; i++) {

            [formData appendPartWithFileData:UIImageJPEGRepresentation(self.uploadImageView.allImages(i),0.8) name:@"images[]" fileName:@"something.jpg" mimeType:@"image/jpeg"];

        }

    } success:^(AFHTTPRequestOperation *operation, id responseObject) {

        if ([[responseObject objectForKey:@"code"] isEqualToNumber:@1]) {

            [SVProgressHUD showSuccessWithStatus:@"发布成功!"];

            [self.navigationController popViewControllerAnimated:YES];

        } else if ([[responseObject objectForKey:@"code"] isEqualToNumber:@400]) {

            [SVProgressHUD dismiss];

        } else {

            [SVProgressHUD dismiss];

            UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:[responseObject objectForKey:@"message"] message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];

            [alertView show];

        }

       

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

        NSLog(@"%@",error);

        [SVProgressHUD dismiss];

        [[[UIAlertView alloc]initWithTitle:@"上传失败" message:@"网络故障,请稍后重试" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil] show];

    }];

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容