Search code examples
ruby-on-railsswiftpaperclipsdwebimage

How to download a picture data in swift that is updated on rails by paperclip?


I am worried about how to set the url in swift.I'd like to download and show the image using SDWebImage on swift. I've set url as following, but it cannot return picture. I cannot find out how to get style and basename automatically.

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

I have been writing server side by RoR and upload the picture by paperclip.

Thank you for your help.

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

Swift

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

Solution

  • Basically what you need is the URL of your picture...Tell your ROR coder to send the URL of the picture in response from API...and if you are using Alamofire you can just simply try this

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