2013-06-20 45 views
1

在我的Rails应用程序中,我使用Paperclip上传照片并将它们存储在S3中。所以我想把这个功能带到我的iOS应用中。我使用this gist在RubyMotion应用中上传图片上传,但速度令人难以置信。在回形针中看到这个日期问题后,我尝试了一种不同的方法:https://github.com/thoughtbot/paperclip/issues/254#issuecomment-321507RubyMotion使用Formotion和BubbleWrap将图像从iOS上传到Rails/S3

因此,我尝试使用BubbleWrap的:form_data:format并传递UIImage.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'] 
    }] 
    }] 
}) 

我的气泡布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 “)?有没有办法通过我传递给服务器的所有二进制数据加速这个过程?

回答

2

我不知道做这样的事情有气泡布,但...

..这里是与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 here

的文档/例子希望它帮助。

+0

看起来像现在是从BubbleWrap转移到AFMotion的时候了。谢谢! – tvalent2

+0

@ tvalent2如果此答案适合您,请将答案标记为已接受! – siekfried

相关问题