I've had a bit of trouble following the docs to add an image to a promotion screen. I am pulling a post
object from a server which contains an image url. I would like to show the post body as well as an image if one exists in the post. My most recent attempt yields the error
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString bytes]: unrecognized selector sent to instance 0xa813810'
when running this code:
class PostScreen < BaseScreen
attr_accessor :post
def on_load
self.title = "#{self.post['person']['name']}'s post"
end
def will_appear
super
add UILabel.new, {
text: self.post['body'],
font: UIFont.systemFontOfSize(16),
left: 20,
top: 100,
width: 280,
height: 300,
text_alignment: NSTextAlignmentCenter,
lineBreakMode: UILineBreakModeWordWrap,
numberOfLines: 0
}
if self.post['images'].length > 0
add UIImageView.new, {
image: UIImage.alloc.initWithData(self.post['images'][0]['image_uploader']['medium_square']['url'])
}
end
end
end
I'm quite new to rubymotion (obviously) so please ask if you need more info to answer the question. I'm assuming this is pretty straightforward but I have to step away for the evening so I thought StackOverflow could take a crack at it.
Thanks!
-------- Update -------------
After some digging, I believe this is closer to correct:
if self.post['images'].length > 0
url = self.post['images'][0]['image_uploader']['medium_square']['url']
v = UIImageView.new
v.setImageWithURL("url")
add v
end
I am using the cocoapod SDWebImage
which I believe gives me that setImageWithURL
method. When I run the app, v.image
is null. When I run that same serious of commands in the console, an image is successfully added to the UIImageView
.
From the rubymotion cookbook (https://github.com/IconoclastLabs/rubymotion_cookbook/blob/master/ch_2/17_imageview/app/root_controller.rb) I was able to get this working
if self.post['images'].length > 0
url = self.post['images'][0]['image_uploader']['medium_square']['url']
image_view = UIImageView.alloc.initWithFrame(self.view.bounds)
image_view.contentMode = UIViewContentModeScaleAspectFit
image_view.center = self.view.center
image_view.setImageWithURL(url)
add image_view
end
I'm sure there are a lot of ways to accomplish my goal and maybe some better. I'd still love for someone to answer this question better than me and I will gladly give you the check mark!