Class: MotionSplash::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/motion-splash/generator.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGenerator

Returns a new instance of Generator.



7
8
9
10
# File 'lib/motion-splash/generator.rb', line 7

def initialize
  @config = Config.new
  @current_index = 0
end

Class Method Details

.generate_snapshotsObject



3
4
5
# File 'lib/motion-splash/generator.rb', line 3

def self.generate_snapshots
  self.new.start
end

Instance Method Details

#create_image_for(scale, view) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/motion-splash/generator.rb', line 50

def create_image_for(scale, view)
  UIGraphicsBeginImageContextWithOptions(view.bounds.size, true, scale)
  view.drawViewHierarchyInRect(view.bounds, afterScreenUpdates: true)
  img = UIGraphicsGetImageFromCurrentImageContext()
  UIGraphicsEndImageContext()
  img
end

#enabled_sizesObject



58
59
60
61
62
63
# File 'lib/motion-splash/generator.rb', line 58

def enabled_sizes
  @enabled_sizes ||= @config.sizes.reject do |size, scale|
    @config.exclude_sizes.include?(size) ||
        @config.exclude_scales.include?(scale)
  end + @config.custom_sizes
end

#name_for(scale, size) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/motion-splash/generator.rb', line 65

def name_for(scale, size)
  case scale.to_i
    when 2
      size_suffix = size.last == 480 ? "" : "-#{size.last.to_i}h"
      "#{@config.image_name}#{size_suffix}@2x"
    when 3
      "#{@config.image_name}-#{size.last.to_i}h@3x"
    else
      @config.image_name
  end
end

#save_image(img, scale, size) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/motion-splash/generator.rb', line 42

def save_image(img, scale, size)
  fileManager = NSFileManager.defaultManager
  image_data = UIImagePNGRepresentation(img)
  image_name = name_for(scale, size)
  image_name = "#{@config.images_dir}/#{image_name}.png"
  fileManager.createFileAtPath(image_name, contents: image_data, attributes: nil)
end

#setup_for(size, scale) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/motion-splash/generator.rb', line 22

def setup_for(size, scale)
  puts "Generating image for #{size}@#{scale}x"
  controller_class = Kernel.const_get(@config.controller_class)
  splash_controller = controller_class.alloc.initWithNibName(nil, bundle: nil)
  splash_controller.splash_generator = self

  @window = UIWindow.alloc.initWithFrame([[0,0], size])
  @window.rootViewController = splash_controller
  @window.makeKeyAndVisible
  splash_controller.view.frame = [[0, 0], size]
end

#setup_next_sizeObject



16
17
18
19
20
# File 'lib/motion-splash/generator.rb', line 16

def setup_next_size
  exit if @current_index >= enabled_sizes.size
  size, scale = enabled_sizes[@current_index]
  setup_for(size, scale)
end

#startObject



12
13
14
# File 'lib/motion-splash/generator.rb', line 12

def start
  setup_next_size
end

#take_snapshotObject



34
35
36
37
38
39
40
# File 'lib/motion-splash/generator.rb', line 34

def take_snapshot
  size, scale = enabled_sizes[@current_index]
  image = create_image_for(scale, @window)
  save_image(image, scale, size)
  @current_index += 1
  setup_next_size
end