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
- #strong_references ⇒ Object
- #update_with_options(new_options = {}) ⇒ Object
Methods included from DrawBackgroundMixin
#draw_background_in_context, #draw_rect_in_context
Methods inherited from DrawElement
#bind_gesture, #computed_frame, #default_padding_for, factory, #hide, #on_container_render, #render!, #rerender!, #show, #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, #cell_element?, #cell_section?, #compute_options!, #computed_options, #dealloc, factory, #hide, #initialize, #notify_section_after_render, #notify_section_before_render, #reload!, #render, #render!, #rerender!, #show, #update, #update_options
Methods included from HasClassFactory
#camelize_factory, #class_factory, #low_camelize_factory, #underscore_factory
Methods included from HasStyleOptions
Methods included from HasStyleChainBuilder
Methods included from HasNormalizer
#debug_info, #element?, #normalize_object, #normalize_options, #normalize_value
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
18 19 20 21 22 23 24 25 26 27 28 |
# File 'motion-prime/elements/draw/image.rb', line 18 def draw_in(rect) return if [:hidden] super if [:draw_in_rect] draw_in_context(UIGraphicsGetCurrentContext()) else draw_background_in_context(UIGraphicsGetCurrentContext()) draw_with_layer end load_image end |
#draw_in_context(context) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'motion-prime/elements/draw/image.rb', line 30 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([:inner_rect], inset, inset) radius = [:corner_radius].to_f if [:corner_radius] && [:masks_to_bounds] UIGraphicsPushContext(context) if radius draw_rect_in_context(context, rect: rect, radius: radius, rounded_corners: [:rounded_corners]) 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 14 15 16 |
# 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), inner_rect: CGRectMake(frame_inner_left, frame_inner_top, frame_width, frame_height) }) end |
#draw_with_layer ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'motion-prime/elements/draw/image.rb', line 54 def draw_with_layer = @layer.try(:removeFromSuperlayer) return unless image = [:image] rect = [:inner_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
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'motion-prime/elements/draw/image.rb', line 79 def load_image return if @loading || image_data || ![:url] @loading = true refs = strong_references BW::Reactor.schedule do return unless refs.all?(&:weakref_alive?) manager = SDWebImageManager.sharedManager manager.downloadWithURL([:url], options: 0, progress: lambda{ |r_size, e_size| }, completed: lambda{ |image, error, type, finished| if !image || !refs.all?(&:weakref_alive?) @loading = false return end if Prime.env.development? && false image_cache = SDImageCache.sharedImageCache image_cache.clearMemory image_cache.clearDisk image_cache.cleanDisk end if [:post_process].present? image = [:post_process][:method].to_proc.call([:post_process][:target], image) end self.image_data = image section.cached_draw_image = nil if section.respond_to?(:cell_section_name) section.pending_display! else self.view.performSelectorOnMainThread :setNeedsDisplay, withObject: nil, waitUntilDone: true end @loading = false } ) end end |
#strong_references ⇒ Object
72 73 74 75 76 77 |
# File 'motion-prime/elements/draw/image.rb', line 72 def strong_references # .compact() is required here, otherwise screen will not be released refs = [section, (section.collection_section if section.respond_to?(:cell_section_name))].compact refs += section.strong_references if section refs end |
#update_with_options(new_options = {}) ⇒ Object
119 120 121 122 123 |
# File 'motion-prime/elements/draw/image.rb', line 119 def ( = {}) self.image_data = nil if .slice(:url, :image).any? || .blank? @layer.try(:removeFromSuperlayer) super end |