class UIImage

Stuff I copied from MOCommon, some new

Constants

M_PI

Public Class Methods

size_at_path(s) click to toggle source
# File lib/purplish-red/non-ui/ui_image.rb, line 5
def self.size_at_path(s)
  imageFileURL = NSURL.fileURLWithPath(s)
  imageSource = CGImageSourceCreateWithURL(imageFileURL, nil)
  if imageSource.nil?
    #p "Error loading image"
  else
    imageProperties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, {KCGImageSourceShouldCache => false})
    if imageProperties.nil?
      #p "Failed to get properties for image"
    else
      return CGSizeMake(imageProperties[KCGImagePropertyPixelWidth], imageProperties[KCGImagePropertyPixelHeight])
    end
  end

  return CGSizeZero
end

Public Instance Methods

height() click to toggle source
# File lib/purplish-red/non-ui/ui_image.rb, line 40
def height
  size.height
end
image_by_cropping_to_center_size(aSize) click to toggle source
# File lib/purplish-red/non-ui/ui_image.rb, line 108
def image_by_cropping_to_center_size(aSize)
  if aSize.width/aSize.height > size.width/size.height
    return image_by_cropping_to_rect(CGRect.new([0, (size.height-aSize.height)/2], [aSize.width, aSize.height]))
  else
    return image_by_cropping_to_rect(CGRect.new([(size.width-aSize.width)/2, 0], [aSize.width, aSize.height]))
  end
end
image_by_cropping_to_rect(aRect) click to toggle source

aRect is assumed to be in the “same scale” as self

# File lib/purplish-red/non-ui/ui_image.rb, line 98
def image_by_cropping_to_rect(aRect)
  if aRect.is_a? CGRect
    cropped = CGImageCreateWithImageInRect(self.CGImage, [[aRect.origin.x*self.scale, aRect.origin.y*self.scale], [aRect.size.width*self.scale, aRect.size.height*self.scale]])
  else
    cropped = CGImageCreateWithImageInRect(self.CGImage, [[aRect[0][0]*self.scale, aRect[0][1]*self.scale], [aRect[1][0]*self.scale, aRect[1][1]*self.scale]])
  end
  UIImage.imageWithCGImage(cropped, scale:self.scale, orientation:self.imageOrientation)
end
rotate_to_correct_orientation() click to toggle source
# File lib/purplish-red/non-ui/ui_image.rb, line 222
def rotate_to_correct_orientation
        rotate_with_orientation(imageOrientation)
end
rotate_with_orientation(anOrientation) click to toggle source
# File lib/purplish-red/non-ui/ui_image.rb, line 117
def rotate_with_orientation(anOrientation)
  #Front camera? We try to make fix it
  if (size.width == 640 && size.height == 480) || (size.width == 480 && size.height == 640)
    p 'Front camera photo'
    if anOrientation == UIImageOrientationUp
      #EXIF = 1
    elsif anOrientation == UIImageOrientationDown
      #EXIF = 3
      anOrientation = UIImageOrientationLeftMirrored
    elsif anOrientation == UIImageOrientationLeft
      #EXIF = 6
    elsif anOrientation == UIImageOrientationRight
      #EXIF = 8
      anOrientation = UIImageOrientationLeftMirrored
    elsif anOrientation == UIImageOrientationUpMirrored
      #EXIF = 2
    elsif anOrientation == UIImageOrientationDownMirrored
      #EXIF = 4
    elsif anOrientation == UIImageOrientationLeftMirrored
      #EXIF = 5
    elsif UIImageOrientationRightMirrored
      #EXIF = 7
      anOrientation = UIImageOrientationRightMirrored
    end
  elsif
    p 'Not front camera photo'
  end 

  imgRef = self.CGImage
  width = CGImageGetWidth(imgRef)
  height = CGImageGetHeight(imgRef)
  transform = CGAffineTransformIdentity
  bounds = CGRect.new([0, 0], [width, height])
  imageSize = CGSize.new(width, height)

  if anOrientation == UIImageOrientationUp
    #EXIF = 1
    transform = CGAffineTransformIdentity
  elsif anOrientation == UIImageOrientationUpMirrored
    #EXIF = 2
    transform = CGAffineTransformMakeTranslation(imageSize.width, 0.0)
    transform = CGAffineTransformScale(transform, -1.0, 1.0)
  elsif anOrientation == UIImageOrientationDown
    #EXIF = 3
    transform = CGAffineTransformMakeTranslation(imageSize.width, imageSize.height)
    transform = CGAffineTransformRotate(transform, M_PI)
  elsif anOrientation == UIImageOrientationDownMirrored
    #EXIF = 4
    transform = CGAffineTransformMakeTranslation(0.0, imageSize.height)
    transform = CGAffineTransformScale(transform, 1.0, -1.0)
  elsif anOrientation == UIImageOrientationLeftMirrored
    #EXIF = 5
    boundHeight = bounds.size.height
    bounds.size.height = bounds.size.width
    bounds.size.width = boundHeight
    transform = CGAffineTransformMakeTranslation(imageSize.height, imageSize.width)
    transform = CGAffineTransformScale(transform, -1.0, 1.0)
    transform = CGAffineTransformRotate(transform, 3.0 * M_PI / 2.0)
  elsif anOrientation == UIImageOrientationLeft
    #EXIF = 6
    boundHeight = bounds.size.height
    bounds.size.height = bounds.size.width
    bounds.size.width = boundHeight
    transform = CGAffineTransformMakeTranslation(0.0, imageSize.width)
    transform = CGAffineTransformRotate(transform, 3.0 * M_PI / 2.0)
  elsif anOrientation == UIImageOrientationRightMirrored
    #EXIF = 7
    boundHeight = bounds.size.height
    bounds.size.height = bounds.size.width
    bounds.size.width = boundHeight
    transform = CGAffineTransformMakeScale(-1.0, 1.0)
    transform = CGAffineTransformRotate(transform, M_PI / 2.0)
  elsif anOrientation == UIImageOrientationRight
    #EXIF = 8
    boundHeight = bounds.size.height
    bounds.size.height = bounds.size.width
    bounds.size.width = boundHeight
    transform = CGAffineTransformMakeTranslation(imageSize.height, 0.0)
    transform = CGAffineTransformRotate(transform, M_PI / 2.0)
  end

  UIGraphicsBeginImageContext(bounds.size)
  context = UIGraphicsGetCurrentContext()

  if anOrientation == UIImageOrientationRight || anOrientation == UIImageOrientationLeft
    CGContextScaleCTM(context, -1, 1)
    CGContextTranslateCTM(context, -height, 0)
  elsif anOrientation == UIImageOrientationLeft || anOrientation == UIImageOrientationLeftMirrored || anOrientation == UIImageOrientationRightMirrored
    CGContextScaleCTM(context, 1, -1)
    CGContextTranslateCTM(context, 0, -width)
  else
    CGContextScaleCTM(context, 1, -1)
    CGContextTranslateCTM(context, 0, -height)
  end

  CGContextConcatCTM(context, transform)

  CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRect.new([0, 0], [width, height]), imgRef)
  imageCopy = UIGraphicsGetImageFromCurrentImageContext()
  UIGraphicsEndImageContext()

  imageCopy
end
scale_aspect_to_fill_size(aSize) click to toggle source

Preserve aspect ratio while scaling to fill. Part of the content may be clipped

# File lib/purplish-red/non-ui/ui_image.rb, line 86
def scale_aspect_to_fill_size(aSize)
  if aSize.width/aSize.height > size.width/size.height
    croppedImg = image_by_cropping_to_center_size(CGSize.new(size.width, (size.width/aSize.width * aSize.height).to_i))
  else
    croppedImg = image_by_cropping_to_center_size(CGSize.new((size.height/aSize.height * aSize.width).to_i, size.height))
  end

  croppedImg.scale_to_size(aSize)
end
scale_aspect_to_maximum_size(aSize) click to toggle source

Preserve aspect ratio while scaling. E.g if aSize = (612,612), the longer side will be 612 and the shorter side will be at most 612

# File lib/purplish-red/non-ui/ui_image.rb, line 62
def scale_aspect_to_maximum_size(aSize)
  if aSize.width/aSize.height > self.size.width/self.size.height
    s = CGSize.new((aSize.height/size.height * size.width).to_i, aSize.height)
  else
    s = CGSize.new(aSize.width, (aSize.width/size.width * size.height).to_i)
  end

  scale_to_size(s)
end
scale_aspect_to_minimum_size(aSize) click to toggle source

Preserve aspect ratio while scaling. E.g if aSize = (612,612), the shorter side will be 612 and the shorter side will be at least 612

# File lib/purplish-red/non-ui/ui_image.rb, line 74
def scale_aspect_to_minimum_size(aSize)
  if aSize.width/aSize.height > self.size.width/self.size.height
    s = CGSize.new(aSize.width, (aSize.width/size.width * size.height).to_i)
  else
    s = CGSize.new((aSize.height/size.height * size.width).to_i, aSize.height)
  end

  scale_to_size(s)
end
scale_aspect_to_size(size) click to toggle source
# File lib/purplish-red/non-ui/ui_image.rb, line 45
def scale_aspect_to_size(size)
  scale_aspect_to_maximum_size(size)
end
scale_to_size(size) click to toggle source
# File lib/purplish-red/non-ui/ui_image.rb, line 50
def scale_to_size(size)
  colorSpace = CGColorSpaceCreateDeviceRGB()

  context = CGBitmapContextCreate(nil, size.width*scale, size.height*scale, 8, size.width*4*scale, colorSpace, KCGImageAlphaPremultipliedLast)
  imageReference = self.CGImage
  CGContextDrawImage(context, CGRectMake(0, 0, size.width*scale, size.height*scale), imageReference)
  copy = CGBitmapContextCreateImage(context)
  UIImage.imageWithCGImage(copy, scale:scale, orientation:imageOrientation)
end
show_globally() click to toggle source
# File lib/purplish-red/non-ui/ui_image.rb, line 227
def show_globally
  btn = self.to_btn
  btn.addTarget(btn, action:'removeFromSuperview', forControlEvents:UIControlEventTouchUpInside)
  parent = App.delegate.window
  btn.backgroundColor = UIColor.redColor
  btn.center_x = parent.width/2
  btn.center_y = parent.height/2
  parent.addSubview(btn)
end
to_btn() click to toggle source
# File lib/purplish-red/non-ui/ui_image.rb, line 23
def to_btn
  b = UIButton.buttonWithType(UIButtonTypeCustom)
  b.setImage(self, forState:UIControlStateNormal)
  b.frame = [[0, 0], [size.width, size.height]]
  b
end
to_imgv() click to toggle source
# File lib/purplish-red/non-ui/ui_image.rb, line 30
def to_imgv
  UIImageView.alloc.initWithImage(self)
end
width() click to toggle source
# File lib/purplish-red/non-ui/ui_image.rb, line 35
def width
  size.width
end