Skip to content

Instantly share code, notes, and snippets.

@bulters
Created March 19, 2013 16:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bulters/5197760 to your computer and use it in GitHub Desktop.
Save bulters/5197760 to your computer and use it in GitHub Desktop.
Rubymotion version of scaleAndRotate for iOS
def scaleAndRotateImage(image)
scaleAndRotateImage(image, maxResolution: 640)
end
def scaleAndRotateImage(image, maxResolution: maxResolution)
imgRef = image.CGImage
width, height = CGImageGetWidth(imgRef), CGImageGetHeight(imgRef)
bounds = CGRectMake(0, 0, width, height)
if (width > maxResolution || height > maxResolution)
ratio = width/height
if ratio > 1
bounds.size.width = maxResolution
bounds.size.height = (bounds.size.width / ratio).round
else
bounds.size.height = maxResolution
bounds.size.width = (bounds.size.height * ratio).round
end
end
scaleRatio = bounds.size.width / width
imageSize = CGSizeMake(width, height)
orientation = image.imageOrientation
transformation = case orientation
when UIImageOrientationUp then
CGAffineTransformIdentity
when UIImageOrientationUpMirrored then
CGAffineTransformScale(CGAffineTransformMakeTranslation(imageSize.width, 0.0), -1.0, 0.0)
when UIImageOrientationDown then
CGAffineTransformRotate(CGAffineTransformMakeTranslation(imageSize.width, imageSize.height), Math::PI)
when UIImageOrientationDownMirrored then
CGAffineTransformScale(CGAffineTransformMakeTranslation(0.0, imageSize.height), 0.0, -1.0)
when UIImageOrientationLeftMirrored then
bounds = swapBounds(bounds)
CGAffineTransformRotate(CGAffineTransformScale(CGAggineTransformMakeTranslation(imageSize.height, imageSize.width), 0.0, imageSize.width), 3.0 * Math::PI / 2.0)
when UIImageOrientationLeft then
bounds = swapBounds(bounds)
CGAffineTransformRotate(CGAffineTransformMakeTranslation(0.0, imageSize.width), 3.0 * Math::PI / 2.0)
when UIImageOrientationRightMirrored then
bounds = swapBounds(bounds)
CGAffineTransformRotate(CGAffineTransformMakeScale(-1.0, 1.0), Math::PI / 2.0)
when UIImageOrientationRight then
bounds = swapBounds(bounds)
CGAffineTransformRotate(CGAggineTransformMakeTranslation(imageSize.height, 0.0), Math::PI / 2.0)
else
CGAffineTransformIdentity
end
UIGraphicsBeginImageContext(bounds.size)
context = UIGraphicsGetCurrentContext()
if [UIImageOrientationRight, UIImageOrientationLeft].include?(orientation)
CGContextScaleCTM(context, -scaleRatio, scaleRatio)
CGContextTranslateCTM(context, -height, 0)
else
CGContextScaleCTM(context, scaleRatio, -scaleRatio)
CGContextTranslateCTM(context, 0, -height)
end
CGContextConcatCTM(context, transformation)
CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, width, height), imgRef)
imageCopy = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
imageCopy
end
def swapBounds(bounds)
CGRectMake(0, 0, bounds.size.height, bounds.size.width)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment