autocrop UIImage from http://stackoverflow.com/questions/9061800/how-do-i-autocrop-a-uiimage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func cropRect() -> CGRect { | |
let cgImage = self.CGImage! | |
let context = createARGBBitmapContextFromImage(cgImage) | |
if context == nil { | |
return CGRectZero | |
} | |
let height = CGFloat(CGImageGetHeight(cgImage)) | |
let width = CGFloat(CGImageGetWidth(cgImage)) | |
let rect = CGRectMake(0, 0, width, height) | |
CGContextDrawImage(context, rect, cgImage) | |
let data = UnsafePointer<CUnsignedChar>(CGBitmapContextGetData(context)) | |
if data == nil { | |
return CGRectZero | |
} | |
var lowX = width | |
var lowY = height | |
var highX: CGFloat = 0 | |
var highY: CGFloat = 0 | |
//Filter through data and look for non-transparent pixels. | |
for (var y: CGFloat = 0 ; y < height ; y++) { | |
for (var x: CGFloat = 0; x < width ; x++) { | |
let pixelIndex = (width * y + x) * 4 /* 4 for A, R, G, B */ | |
if data[Int(pixelIndex)] != 0 { //Alpha value is not zero pixel is not transparent. | |
if (x < lowX) { | |
lowX = x | |
} | |
if (x > highX) { | |
highX = x | |
} | |
if (y < lowY) { | |
lowY = y | |
} | |
if (y > highY) { | |
highY = y | |
} | |
} | |
} | |
} | |
return CGRectMake(lowX, lowY, highX-lowX, highY-lowY) | |
} | |
// The method to create the Bitmap Context: | |
func createARGBBitmapContextFromImage(inImage: CGImageRef) -> CGContextRef? { | |
let width = CGImageGetWidth(inImage) | |
let height = CGImageGetHeight(inImage) | |
let bitmapBytesPerRow = width * 4 | |
let bitmapByteCount = bitmapBytesPerRow * height | |
let colorSpace = CGColorSpaceCreateDeviceRGB() | |
if colorSpace == nil { | |
return nil | |
} | |
let bitmapData = malloc(bitmapByteCount) | |
if bitmapData == nil { | |
return nil | |
} | |
let context = CGBitmapContextCreate (bitmapData, | |
width, | |
height, | |
8, // bits per component | |
bitmapBytesPerRow, | |
colorSpace, | |
CGImageAlphaInfo.PremultipliedFirst.rawValue) | |
return context | |
} | |
// example | |
let image = // UIImage Source | |
let newRect = image.cropRect() | |
if let imageRef = CGImageCreateWithImageInRect(image.CGImage!, newRect) { | |
let newImage = UIImage(CGImage: imageRef) | |
// Use this new Image | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment