Class: MotionPrime::ImageDrawElement

Inherits:
DrawElement show all
Includes:
DrawBackgroundMixin
Defined in:
motion-prime/elements/draw/image.rb

Instance Attribute Summary collapse

Attributes inherited from BaseElement

#name, #options, #screen, #section, #styles, #view, #view_class, #view_name

Instance Method Summary collapse

Methods included from DrawBackgroundMixin

#draw_background_in_context

Methods inherited from DrawElement

#bind_gesture, #computed_frame, #default_padding_for, factory, #hide, #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

#calculate_frame_for

Methods inherited from BaseElement

#add_target, after_render, before_render, #bind_gesture, #cell_element?, #cell_section?, #compute_options!, #computed_options, #dealloc, factory, #hide, #initialize, #reload!, #render, #render!, #rerender!, #show, #update, #update_options

Methods included from HasClassFactory

#camelize_factory, #class_factory, #low_camelize_factory

Methods included from HasStyleChainBuilder

#build_styles_chain

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_dataObject

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
22
23
24
25
# File 'motion-prime/elements/draw/image.rb', line 15

def draw_in(rect)
  return if computed_options[:hidden]
  draw_background_in_context(UIGraphicsGetCurrentContext())
  # bug - when it's a CollectionView we get invalid frame when drawing in a layer
  if computed_options[:draw_in_rect] || view.is_a?(MPCollectionCellWithSection)
    draw_in_context(UIGraphicsGetCurrentContext())
  else
    draw_with_layer
  end
  load_image
end

#draw_in_context(context) ⇒ Object



27
28
29
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 27

def draw_in_context(context)
  return if computed_options[:hidden]

  draw_background_in_context(context)
  options = draw_options
  return unless image = options[:image]

  border_width = options[:border_width]
  inset = border_width > 0 ? (border_width - 1 ).abs*0.5 : 0
  rect = CGRectInset(options[:rect], inset, inset)
  radius = options[:corner_radius].to_f if options[:corner_radius] && options[: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_optionsObject



7
8
9
10
11
12
13
# File 'motion-prime/elements/draw/image.rb', line 7

def draw_options
  image = image_data || computed_options[:image]
  image ||= computed_options[:default] if computed_options[:url]

  # already initialized image or image from resources or default image
  super.merge({image: image.try(:uiimage)})
end

#draw_with_layerObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'motion-prime/elements/draw/image.rb', line 54

def draw_with_layer
  options = draw_options
  @layer.removeFromSuperlayer if @layer
  return unless image = options[:image]
  rect = options[:rect]
  radius = options[:corner_radius].to_f if options[:corner_radius] && options[:masks_to_bounds]

  @layer = CALayer.layer
  @layer.contents = image.CGImage
  @layer.frame = rect
  @layer.bounds = rect

  @layer.masksToBounds = options[:masks_to_bounds]
  @layer.cornerRadius = radius if radius
  view.layer.addSublayer(@layer)
end

#load_imageObject



75
76
77
78
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
# File 'motion-prime/elements/draw/image.rb', line 75

def load_image
  return if @loading || image_data || !computed_options[:url]
  @loading = true

  ref_key = allocate_strong_references
  BW::Reactor.schedule do
    manager = SDWebImageManager.sharedManager
    manager.downloadWithURL(computed_options[:url],
      options: 0,
      progress: lambda{ |r_size, e_size|  },
      completed: lambda{ |image, error, type, finished|
        if !image || allocated_references_released?
          @loading = false
          release_strong_references(ref_key)
          return
        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: false
        end
        @loading = false
        release_strong_references(ref_key)
      }
    )
  end
end

#strong_referencesObject



71
72
73
# File 'motion-prime/elements/draw/image.rb', line 71

def strong_references
  [section, (section.collection_section if section.respond_to?(:cell_section_name))].compact
end

#update_with_options(new_options = {}) ⇒ Object



107
108
109
110
# File 'motion-prime/elements/draw/image.rb', line 107

def update_with_options(new_options = {})
  super
  self.image_data = nil if new_options.slice(:url, :image).any?
end