美文网首页
file类型图片上传

file类型图片上传

作者: iOS菜鸟攻城狮 | 来源:发表于2018-05-17 11:22 被阅读65次

    不需要考虑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];

        }];

    相关文章

      网友评论

          本文标题:file类型图片上传

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