Method: UIImage#scale_within

Defined in:
lib/sugarcube-image/uiimage.rb

#scale_within(new_size) ⇒ Object

This method is similar to scale_to, except it doesn't pad the image, it just scales the image so that it will fit inside the new bounds.



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/sugarcube-image/uiimage.rb', line 185

def scale_within(new_size)
  target_size = SugarCube::CoreGraphics::Size(new_size)
  image_size = self.size

  if CGSizeEqualToSize(target_size, self.size)
    return self
  end

  width = image_size.width
  height = image_size.height

  target_width = target_size.width
  target_height = target_size.height

  width_factor = target_width / width
  height_factor = target_height / height

  if width_factor < height_factor
    scale_factor = width_factor
  else
    scale_factor = height_factor
  end

  if scale_factor == 1
    return self
  end

  scaled_size = CGSize.new(width * scale_factor, height * scale_factor)
  return scale_to(scaled_size)
end