Class: UIImage

Inherits:
Object show all
Defined in:
lib/sugarcube/uiimage.rb

Instance Method Summary collapse

Instance Method Details

#darken(options = {}) ⇒ Object

Accepts two options: brightness (default: -0.5) and saturation (default: -0.2) Returns a darkened version of the image.

Raises:

  • (Exception)


111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/sugarcube/uiimage.rb', line 111

def darken(options={})
  filter_name = 'CIColorControls'
  filter_options = {
    inputBrightness: options[:brightness] || -0.5,
    inputSaturation: options[:saturation] || -0.2,
  }

  cg_input_image = CIImage.alloc.initWithImage(self)
  darken_filter = CIFilter.filterWithName(filter_name)
  raise Exception.new("Filter not found: #{filter_name}") unless darken_filter

  darken_filter.setDefaults
  darken_filter.setValue(cg_input_image, forKey:'inputImage')
  filter_options.each_pair do |key, value|
    darken_filter.setValue(value, forKey:key)
  end
  output = darken_filter.valueForKey('outputImage')

  context = CIContext.contextWithOptions(nil)
  cg_output_image = context.createCGImage(output, fromRect:output.extent)
  output_image = UIImage.imageWithCGImage(cg_output_image)

  return output_image
end

#in_rect(rect) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/sugarcube/uiimage.rb', line 30

def in_rect(rect)
  # not necessary, since we don't modify/examine the rect
  # rect = SugarCube::CoreGraphics::Rect(rect)
  imageRef = CGImageCreateWithImageInRect(self.CGImage, rect)
  sub_image = UIImage.imageWithCGImage(imageRef)

  return sub_image
end

#nsdataNSData

Returns an NSData object in PNG format.

Returns:

  • (NSData)

    an NSData object in PNG format



20
21
22
# File 'lib/sugarcube/uiimage.rb', line 20

def nsdata
  UIImagePNGRepresentation(self)
end

#rotate(angle_or_direction) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/sugarcube/uiimage.rb', line 139

def rotate(angle_or_direction)
  case angle_or_direction
  when :left
    radian = -90.degrees
  when :right
    radian = 90.degrees
  when :flip
    radian = 180.degrees
  when Numeric
    radian = angle_or_direction
  else
    raise "Unknown angle/direction #{angle_or_direction.inspect}"
  end

  w = (self.size.width * Math.cos(radian)).abs + (self.size.height * Math.sin(radian)).abs
  h = (self.size.height * Math.cos(radian)).abs + (self.size.width * Math.sin(radian)).abs
  new_size = CGSize.new(w, h)
  new_size = self.size

  # Create the bitmap context
  UIGraphicsBeginImageContext(new_size)
  bitmap = UIGraphicsGetCurrentContext()

  # Move the origin to the middle of the image so we will rotate and scale around the center.
  CGContextTranslateCTM(bitmap, new_size.width / 2, new_size.height / 2)

  # Rotate the image context
  CGContextRotateCTM(bitmap, radian)

  # otherwise it'll be upside down:
  CGContextScaleCTM(bitmap, 1.0, -1.0)
  # Now, draw the rotated/scaled image into the context
  CGContextDrawImage(bitmap, CGRectMake(-new_size.width / 2, -new_size.height / 2, new_size.width, new_size.height), self.CGImage)

  new_image = UIGraphicsGetImageFromCurrentImageContext()
  UIGraphicsEndImageContext()
  return new_image
end

#rounded(corner_radius = 5) ⇒ Object



99
100
101
102
103
104
105
106
107
# File 'lib/sugarcube/uiimage.rb', line 99

def rounded(corner_radius=5)
  UIGraphicsBeginImageContext(size)
  path = UIBezierPath.bezierPathWithRoundedRect([[0, 0], size], cornerRadius:corner_radius)
  path.addClip
  self.drawInRect([[0, 0], size])
  new_image = UIGraphicsGetImageFromCurrentImageContext()
  UIGraphicsEndImageContext()
  return new_image
end

#scale_to(new_size) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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
95
96
97
# File 'lib/sugarcube/uiimage.rb', line 39

def scale_to(new_size)
  new_size = SugarCube::CoreGraphics::Size(new_size)

  sourceImage = self
  newImage = nil

  image_size = sourceImage.size
  width = image_size.width
  height = image_size.height

  target_width = new_size.width
  target_height = new_size.height

  scale_factor = 0.0
  scaled_width = target_width
  scaled_height = target_height

  thumbnail_point = CGPoint.new(0.0, 0.0)

  unless CGSizeEqualToSize(image_size, new_size)
    width_factor = target_width / width
    heightFactor = target_height / height

    if width_factor < heightFactor
      scale_factor = width_factor
    else
      scale_factor = heightFactor
    end

    scaled_width  = width * scale_factor
    scaled_height = height * scale_factor

    # center the image

    if width_factor < heightFactor
      thumbnail_point.y = (target_height - scaled_height) * 0.5
    elsif width_factor > heightFactor
      thumbnail_point.x = (target_width - scaled_width) * 0.5
    end
  end

  # this is actually the interesting part:

  UIGraphicsBeginImageContext(new_size)

  thumbnail_rect = CGRectZero
  thumbnail_rect.origin = thumbnail_point
  thumbnail_rect.size.width  = scaled_width
  thumbnail_rect.size.height = scaled_height

  sourceImage.drawInRect(thumbnail_rect)

  new_image = UIGraphicsGetImageFromCurrentImageContext()
  UIGraphicsEndImageContext()

  raise "could not scale image"  unless new_image

  return new_image
end

#stretchable(insets = UIEdgeInsetsZero) ⇒ Object



187
188
189
190
191
# File 'lib/sugarcube/uiimage.rb', line 187

def stretchable(insets=UIEdgeInsetsZero)
  # not necessary, since we don't modify/examine the insets
  # insets = SugarCube::CoreGraphics::EdgeInsets(insets)
  resizableImageWithCapInsets(insets, resizingMode:UIImageResizingModeStretch)
end

#tileable(insets = UIEdgeInsetsZero) ⇒ Object



181
182
183
184
185
# File 'lib/sugarcube/uiimage.rb', line 181

def tileable(insets=UIEdgeInsetsZero)
  # not necessary, since we don't modify/examine the insets
  # insets = SugarCube::CoreGraphics::EdgeInsets(insets)
  resizableImageWithCapInsets(insets, resizingMode:UIImageResizingModeTile)
end

#uicolor(alpha = nil) ⇒ UIColor

Returns:



5
6
7
8
9
10
11
12
# File 'lib/sugarcube/uiimage.rb', line 5

def uicolor(alpha=nil)
  color = UIColor.colorWithPatternImage(self)
  if not alpha.nil?
    color = color.colorWithAlphaComponent(alpha.to_f)
  end

  color
end

#uiimageObject



2
# File 'lib/sugarcube/uiimage.rb', line 2

def uiimage ; self ; end

#uiimageviewUIImageView

Returns:

  • (UIImageView)


15
16
17
# File 'lib/sugarcube/uiimage.rb', line 15

def uiimageview
  UIImageView.alloc.initWithImage(self)
end