2015-07-19 29 views
0

我很擔心如何在swift中設置網址。我想在swift上使用SDWebImage下載並顯示圖像。我已將url設置爲如下,但無法返回圖片。我找不到如何自動獲取樣式和基本名稱。如何通過回形針在rails上更新swift中的圖片數據?

http://localhost:3000/assets/photos/:id/:style/:basename.:extension 

我一直在寫服務器端的RoR,並通過回形針上傳圖片。

謝謝你的幫助。

Rails的

class User < ActiveRecord::Base 
    has_many :questions 
    has_many :answers 

    has_attached_file :photo, 
    :styles => { medium: "300x300>", thumb: "100x100>" }, 
    :url => "/assets/photos/:id/:style/:basename.:extension", 
    :path => "#{Rails.root}/public/assets/photos/:id/:style/:basename.:extension" 
    validates_attachment :photo, content_type: { content_type: ["image/jpg", "image/jpeg", "image/png", "image/gif"] } 
end 

斯威夫特

let cell = tableView.dequeueReusableCellWithIdentifier("iconIdentifier", forIndexPath: indexPath) as! IconTableViewCell 
let imageURL = NSURL(string: "http://localhost:3000/assets/photos/:id/:style/:basename.:extension") 
     cell.iconView.sd_setImageWithURL(imageURL!) 
return cell 

回答

1

基本上你需要的是你的圖片的URL ...告訴你的ROR編碼器發送的圖片的URL,從API響應.. 。如果你正在使用Alamofire你可以只是試試這個

Alamofire.request(.GET, "https://robohash.org/123.png").response { (request, response, data, error) in 
     self.myImageView.image = UIImage(data: data, scale:1) 
    } 
0

我已經解決了闕通過使用jbuilder,我有了照片的路徑。然後我通過SDWebImage顯示了這張照片。

user.rb

resource :users do 
    desc "return a document" 
    params do 
    requires :id, type:Integer 
    end 
    post ':get_picture', jbuilder:'users/picture' do 
    @user = User.find(params[:id]) 
    end 
end 

picture.jbuilder

JSON。(@用戶,:照片)

相關問題