如何使用Alamofire将具有某些键和值的UIImage上传到服务器

时间:2018-08-18 12:20:22

标签: ios swift alamofire

我正在使用alamofire将图像以及一些参数上传到服务器,但是我如何雄心勃勃地引用成员上传错误。

可能是什么问题?我的代码是。

public var selectedImage: UIImage?
...
let parameters: Parameters = ["user_id":  "1","description": "test"];
    Alamofire.upload(multipartFormData: { multipartFormData in
        multipartFormData.append(selectedImage, withName: "fileset",fileName: "file.jpg", mimeType: "image/jpg")
        for (key, value) in parameters {
            multipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key)
        } //Optional for extra parameters
    }, to:"mysite/upload.php")
        .uploadProgress { progress in // main queue by default
            print("Upload Progress: \(progress.fractionCompleted)")
        }
        .downloadProgress { progress in // main queue by default
            print("Download Progress: \(progress.fractionCompleted)")
        }
        .responseJSON { response in
            debugPrint(response)
    }

2 个答案:

答案 0 :(得分:1)

尝试一下对我来说很好。

<Style x:Key="myButton" TargetType="{x:Type Button}">
                <Setter Property="Background" Value="#323843" />
                <Setter Property="Height" Value="40" />
                <Setter Property="UseLayoutRounding" Value="True" />
                <Setter Property="HorizontalContentAlignment" Value="Left"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type Button}">
                            <Border x:Name="border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="0" CornerRadius="2" Background="{TemplateBinding Background}" UseLayoutRounding="{TemplateBinding UseLayoutRounding}">
                                <ContentPresenter Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}" Margin="{TemplateBinding Padding}" VerticalAlignment="Center" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" />
                            </Border>
                            <ControlTemplate.Triggers>
                                <Trigger Property="IsMouseOver" Value="true">
                                    <Setter TargetName="border" Property="Background" Value="#2b303a" />
                                    <Setter Property="Cursor" Value="Hand"/>
                                </Trigger>
                                <Trigger Property="IsMouseOver" Value="False">
                                    <Setter TargetName="border" Property="Background" Value="#323843" />
                                    <Setter Property="Cursor" Value="Hand"/>
                                </Trigger>
                                <Trigger Property="IsFocused" Value="True">
                                    <Setter TargetName="border" Property="Background" Value="#2b303a" />
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>

答案 1 :(得分:0)

您正在上传UIImage本身!这是不正确的。

相反,您必须先将其转换为数据,然后再上传...

因此在这行代码之前

multipartFormData.append(selectedImage, withName: "fileset",fileName: "file.jpg", mimeType: "image/jpg")

尝试从所选图像中获取数据:

var imageData : Data?
    if let image = selectedImage{
        imageData = UIImageJPEGRepresentation(image , 0.7)!
    }

然后您可以上传它:

if let data = imageData{

            multipartFormData.append(data,"fileset",fileName: "file.jpg", mimeType: "image/jpg")
        }