jamonholmgren / ProMotion

ProMotion is a RubyMotion gem that makes iPhone development less like Objective-C and more like Ruby.
MIT License
1.26k stars 147 forks source link

Lazy UIImage loading #273

Closed bitgangsta closed 11 years ago

bitgangsta commented 11 years ago

We pass image options around a bunch, and as nice as this is, it's a but nasty to constantly check and see if we have a String or UIImage.

I suggest we add a small function to the String class called to_image. And then create a helper function like:

class String
  def to_image
    UIImage.imageNamed(self)
  end
end

And a helper function such as:

def image_for(name)
  if name.respond_to?(:to_image)
    return name.to_image
  end
  name
end

Then we can wrap any image arguments with calls to the helper function. This also lets us accept other types (besides strings) in the future, as long as they respond to to_image. Sounds pretty flexible to me... what do you guys think?

jamonholmgren commented 11 years ago

Right now, we've avoided all monkey-patching in PM, and I'd like to keep it that way for compatibility with other gems. I think there's already a to_image method on String in some other gems.

Instead, this could be pretty simple:

def image_for(name)
  name.is_a?(UIImage) ? name : UIImage.imageNamed(name)
end
jamonholmgren commented 11 years ago

This unfortunately doesn't allow other types, though.

silasjmatson commented 11 years ago

How about this:

class Imagify
  def initialize(name)
    return name if name.kind_of? UIImage

    # possibly handle other cases...
    UIImage.imageNamed(name)
  end
end

This would allow for more confident programming throughout the gem as all those checks for image/string can be replaced with Imagify.new(string) or Imagify.new(image), and we always get what we expect.

markrickert commented 11 years ago

Sugarcube includes this functionality - https://github.com/rubymotion/sugarcube/blob/master/lib/sugarcube-uikit/nsstring.rb#L3-L8

I agree with @jamonholmgren that we should be patching the base classes as little as possible, especially when there are other gems you can use in tandem with ProMotion that supply the requested functionality.

jamonholmgren commented 11 years ago

I agree with @bitgangsta that what we're doing is kinda yuk, though.