Class: MotionPrime::ImageDrawElement
- Inherits:
-
DrawElement
- Object
- BaseElement
- DrawElement
- MotionPrime::ImageDrawElement
- Includes:
- DrawBackgroundMixin
- Defined in:
- motion-prime/elements/draw/image.rb
Instance Attribute Summary collapse
-
#image_data ⇒ Object
Returns the value of attribute image_data.
Attributes inherited from BaseElement
#name, #options, #screen, #section, #styles, #view, #view_class, #view_name
Instance Method Summary collapse
- #draw_in(rect) ⇒ Object
- #draw_in_context(context) ⇒ Object
- #draw_options ⇒ Object
- #draw_with_layer ⇒ Object
- #load_image ⇒ Object
Methods included from DrawBackgroundMixin
Methods inherited from DrawElement
#bind_gesture, #computed_bottom, #computed_frame, #computed_height, #computed_inner_bottom, #computed_inner_left, #computed_inner_right, #computed_inner_top, #computed_left, #computed_max_height, #computed_max_width, #computed_outer_height, #computed_outer_width, #computed_right, #computed_top, #computed_width, #default_padding_for, factory, #hide, #render!, #show, #update_with_options, #view
Methods included from ElementContentPaddingMixin
#cached_content_outer_height, #cached_content_outer_width, #content_outer_height, #content_outer_width, #content_padding_bottom, #content_padding_height, #content_padding_left, #content_padding_right, #content_padding_top, #content_padding_width, #default_padding_for
Methods included from FrameCalculatorMixin
Methods inherited from BaseElement
#add_target, after_render, before_render, #bind_gesture, #compute_options!, #computed_options, #dealloc, factory, #hide, #initialize, #render, #render!, #show, #update_with_options
Methods included from HasClassFactory
#camelize_factory, #class_factory, #low_camelize_factory
Methods included from HasStyleChainBuilder
Methods included from HasNormalizer
#normalize_object, #normalize_options
Constructor Details
This class inherits a constructor from MotionPrime::BaseElement
Instance Attribute Details
#image_data ⇒ Object
Returns the value of attribute image_data.
5 6 7 |
# File 'motion-prime/elements/draw/image.rb', line 5 def image_data @image_data end |
Instance Method Details
#draw_in(rect) ⇒ Object
15 16 17 18 19 20 21 |
# File 'motion-prime/elements/draw/image.rb', line 15 def draw_in(rect) return if [:hidden] draw_background_in_context(UIGraphicsGetCurrentContext()) [:draw_in_rect] ? draw_in_context(UIGraphicsGetCurrentContext()) : draw_with_layer load_image end |
#draw_in_context(context) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'motion-prime/elements/draw/image.rb', line 23 def draw_in_context(context) return if [:hidden] draw_background_in_context(context) = return unless image = [:image] border_width = [:border_width] inset = border_width > 0 ? (border_width - 1 ).abs*0.5 : 0 rect = CGRectInset([:rect], inset, inset) radius = [:corner_radius].to_f if [:corner_radius] && [:masks_to_bounds] UIGraphicsPushContext(context) if radius CGContextBeginPath(context) CGContextAddArc(context, rect.origin.x + rect.size.width/2, rect.origin.y + rect.size.height/2, radius, 0, 2*Math::PI, 0) # FIXME CGContextClosePath(context) CGContextSaveGState(context) CGContextClip(context) image.drawInRect(rect) CGContextRestoreGState(context) else image.drawInRect(rect) end UIGraphicsPopContext() end |
#draw_options ⇒ Object
7 8 9 10 11 12 13 |
# File 'motion-prime/elements/draw/image.rb', line 7 def image = image_data || [:image] image ||= [:default] if [:url] # already initialized image or image from resources or default image super.merge({image: image.try(:uiimage)}) end |
#draw_with_layer ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'motion-prime/elements/draw/image.rb', line 50 def draw_with_layer = return unless image = [:image] rect = [:rect] radius = [:corner_radius].to_f if [:corner_radius] && [:masks_to_bounds] layer = CALayer.layer layer.contents = image.CGImage layer.frame = rect layer.bounds = rect layer.masksToBounds = [:masks_to_bounds] layer.cornerRadius = radius if radius view.layer.addSublayer(layer) end |
#load_image ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'motion-prime/elements/draw/image.rb', line 67 def load_image return if image_data || ![:url] # TODO: why so many references? @strong_refs = section.strong_references + [screen.main_controller.strong_ref] BW::Reactor.schedule do manager = SDWebImageManager.sharedManager manager.downloadWithURL([:url], options: 0, progress: lambda{ |r_size, e_size| }, completed: lambda{ |image, error, type, finished| if !image || screen.main_controller.retainCount == 1 || section.retainCount == 1 @strong_refs = nil return end self.image_data = image section.cached_draw_image = nil if section.respond_to?(:cell_name) section.pending_display! else self.view.performSelectorOnMainThread :setNeedsDisplay, withObject: nil, waitUntilDone: false end @strong_refs = nil } ) end end |