Class: Motion::LaunchImages

Inherits:
Object
  • Object
show all
Defined in:
lib/motion/launch_images.rb

Class Method Summary collapse

Class Method Details

.capture!Object



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
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/motion/launch_images.rb', line 24

def self.capture!
  screen = UIScreen.mainScreen
  height = screen.bounds.size.height
  scale = begin
    if !screen.respondsToSelector('displayLinkWithTarget:selector:')
      ''
    elsif screen.scale == 1.0
      ''
    elsif screen.scale == 2.0
      '@2x'
    elsif screen.scale == 3.0
      '@3x'
    else
      puts 'Error: Unable to determine screen scale'
      exit
    end
  end
  filename = generate_filename(height, scale)

  if scale != ''
    UIGraphicsBeginImageContextWithOptions(window.bounds.size, false, screen.scale)
  else
    UIGraphicsBeginImageContext(window.bounds.size)
  end

  window.layer.renderInContext(UIGraphicsGetCurrentContext())
  image = UIGraphicsGetImageFromCurrentImageContext()
  UIGraphicsEndImageContext()
  data = UIImagePNGRepresentation(image)
  data.writeToFile(filename, atomically:true)
  puts "Wrote to #{filename}"

  if should_rotate_and_take_again?(height)
    take!(:landscape)
  else
    exit
  end
end

.generate_filename(height, scale) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/motion/launch_images.rb', line 63

def self.generate_filename(height, scale)
  filename = screenshot_path + 'Default'

  case height
  when 480 # iPhone 4s
  when 568 # iPhone 5s
    filename << '-568h'
  when 667 # iPhone 6
    filename << '-667h'
  when 736 # iPhone 6 Plus
    filename << '-736h'
  when 768 # iPad (Landscape)
    filename << '-Landscape'
  when 1024 # iPad (Portrait)
    filename << '-Portrait'
  else
    puts "Error: Invalid screen height #{height}"
    exit
  end

  filename << scale << '.png'
end

.needs_rotation?(orientation) ⇒ Boolean

Returns:

  • (Boolean)


113
114
115
116
117
118
119
120
121
122
123
# File 'lib/motion/launch_images.rb', line 113

def self.needs_rotation?(orientation)
  status = UIApplication.sharedApplication.statusBarOrientation
  if orientation == :portrait && status == UIInterfaceOrientationPortrait
    false
  elsif orientation == :landscape && (status == UIInterfaceOrientationLandscapeLeft ||
                                      status == UIInterfaceOrientationLandscapeRight)
    false
  else
    true
  end
end

.rotate(to) ⇒ Object



103
104
105
106
107
108
109
110
111
# File 'lib/motion/launch_images.rb', line 103

def self.rotate(to)
  if to == :portrait
    value = NSNumber.numberWithInt(UIInterfaceOrientationPortrait)
  else
    value = NSNumber.numberWithInt(UIInterfaceOrientationLandscapeLeft)
  end

  UIDevice.currentDevice.setValue(value, forKey:'orientation')
end

.screenshot_pathObject



3
4
5
# File 'lib/motion/launch_images.rb', line 3

def self.screenshot_path
  NSBundle.mainBundle.objectForInfoDictionaryKey('screenshot_path')
end

.should_rotate_and_take_again?(height) ⇒ Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/motion/launch_images.rb', line 99

def self.should_rotate_and_take_again?(height)
  height == 1024 # iPad (Portrait)
end

.take!(orientation = :portrait) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/motion/launch_images.rb', line 11

def self.take!(orientation = :portrait)
  return unless taking?

  Dispatch::Queue.main.async do
    if needs_rotation?(orientation)
      rotate(orientation)
      sleep 1.0
    end

    Dispatch::Queue.main.async { capture! }
  end
end

.taking?Boolean

Returns:

  • (Boolean)


7
8
9
# File 'lib/motion/launch_images.rb', line 7

def self.taking?
  screenshot_path != nil
end

.windowObject

ported from BubbleWrap to reduce dependencies



87
88
89
90
91
92
93
94
95
96
97
# File 'lib/motion/launch_images.rb', line 87

def self.window
  normal_windows = UIApplication.sharedApplication.windows.select { |w|
    w.windowLevel == UIWindowLevelNormal
  }

  key_window = normal_windows.select { |w|
    w == UIApplication.sharedApplication.keyWindow
  }.first

  key_window || normal_windows.first
end