1

在我的 Rails 应用程序中,我使用 Paperclip 上传照片并将它们存储在 S3 中。所以我想把这个功能带到我的 iOS 应用程序中。我使用这个 gist在我的 RubyMotion 应用程序中上传图像,但速度非常慢。在 Paperclip 中看到这个过时的问题后,我尝试了一种不同的方法:https ://github.com/thoughtbot/paperclip/issues/254#issuecomment-321507 。

所以我尝试使用 BubbleWrap's:form_data :formatUIImage.UIImageJPEGRepresentation(@form.render[:photo], 1)改为通过,看看这是否会加快速度。但是它不起作用。似乎选择的任何照片实际上都没有正确渲染,因为我在我的服务器中没有看到任何照片参数。的输出UIImage.UIImageJPEGRepresentation(@form.render[:photo], 1)看起来不正确。

我的 Formotion 表格:

@form = Formotion::Form.new({
  title: "Settings",
  sections: [{
    title: "Personal Information",
    rows: [{
      title: "Photo",
      type: :image,
      key: :photo,
      value: @profile_photo
    }, {
      title: "Name",
      type: :string,
      placeholder: "Name",
      key: :name,
      value: @profile['name']
    }]
  }]
})

我的 BubbleWrap PUT 更新配置文件:

profile_photo = UIImage.UIImageJPEGRepresentation(@form.render[:photo], 1)

data = {
  'profile[name]'    => @form.render[:name],
  'profile[photo]'   => profile_photo,
  'user[api_token]'  => CONFIG.user.api_token,
  _method:              'PUT'
}

BW::HTTP.post("#{DEFAULT_URL}/profiles/#{CONFIG.user.profile_id}", { format: :form_data, payload: data }) do |response|
  parsed_response = BW::JSON.parse(response.body.to_str)
  if response.ok?
    @data = parsed_response
    self.dismissViewControllerAnimated(true, completion:lambda { parent.load_profile() })
  else
    App.alert("#{parsed_response.first}")
  end
end

所以我的问题是:我必须像要点建议的那样用 .pack("m0") 对图像进行编码吗?有没有办法用我传递给我的服务器的所有二进制数据来加速这个过程?

4

1 回答 1

2

我不知道用 BubbleWrap 做这样的事情,但是......

.. 这是一个使用 AFMotion(它是 AFNetworking 的包装器)上传文件的示例。

client = AFMotion::Client.build("your endpoint") do
  header "Accept", "application/json"
  operation :json
end

image = my_function.get_image
data = UIImagePNGRepresentation(image)

client.multipart.post("avatars") do |result, form_data|
  if form_data
    # Called before request runs
    # see: https://github.com/AFNetworking/AFNetworking/wiki/AFNetworking-FAQ
    form_data.appendPartWithFileData(data, name: "avatar", fileName:"avatar.png", mimeType: "image/png")
  elsif result.success?
    ...
  else
    ...
  end
end

您可能想在此处查看AFMotion 的文档/示例

希望能帮助到你。

于 2013-06-20T06:42:27.647 回答