Open shaikhtariq11 opened 9 years ago
I'm sorry, you can not with this version of the code. But fell free to extend my code and send a PR
Please let me know can i get just cropped image without clear background as a full cropped image. Right now my image is cropping but returning the full image area with crop.
ped one.
I want it should come with full image part. image is centered aligned. Thank You.
Did you find any solution for this situation?
Sorry, I am not working on it. Feel free to solve it and send a PR or fork this project and improve it. This is open source.
Now I find solution, crop your image with transparent option then remove transparent parts of image with this extension :
`extension UIImage {
func cropImageByAlpha() -> UIImage {
let cgImage = self.cgImage
let context = createARGBBitmapContextFromImage(inImage: cgImage!)
let height = cgImage!.height
let width = cgImage!.width
var rect: CGRect = CGRect(x: 0, y: 0, width: CGFloat(width), height: CGFloat(height))
context?.draw(cgImage!, in: rect)
let pixelData = self.cgImage!.dataProvider!.data
let data: UnsafePointer<UInt8> = CFDataGetBytePtr(pixelData)
var minX = width
var minY = height
var maxX: Int = 0
var maxY: Int = 0
//Filter through data and look for non-transparent pixels.
for y in 0..<height {
for x in 0..<width {
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 < minX) {
minX = x
}
if (x > maxX) {
maxX = x
}
if (y < minY) {
minY = y
}
if (y > maxY) {
maxY = y
}
}
}
}
rect = CGRect( x: CGFloat(minX), y: CGFloat(minY), width: CGFloat(maxX-minX), height: CGFloat(maxY-minY))
let imageScale:CGFloat = self.scale
let cgiImage = self.cgImage?.cropping(to: rect)
return UIImage(cgImage: cgiImage!, scale: imageScale, orientation: self.imageOrientation)
}
private func createARGBBitmapContextFromImage(inImage: CGImage) -> CGContext? {
let width = cgImage!.width
let height = cgImage!.height
let bitmapBytesPerRow = width * 4
let bitmapByteCount = bitmapBytesPerRow * height
let colorSpace = CGColorSpaceCreateDeviceRGB()
let bitmapData = malloc(bitmapByteCount)
if bitmapData == nil {
return nil
}
let context = CGContext (data: bitmapData, width: width, height: height, bitsPerComponent: 8, bytesPerRow: bitmapBytesPerRow, space: colorSpace, bitmapInfo: CGImageAlphaInfo.premultipliedFirst.rawValue)
return context
}
}`
So you can use it like this :
guard let resultImageRef = cropperImageView.getCroppedImage(withTransparentBorders: true) else { return } let resultImage = resultImageRef.cropImageByAlpha()
I'm not good in Obj-c thats why I cant send pull request, if you can convert this extension to obj-c than add it to your framework. Thank you for your support
I'm not good in Obj-c thats why I cant send pull request, if you can convert this extension to obj-c than add it to your framework. Thank you for your support
It's works!!!
Thank you for your sharing, can I use these code to create a new project ? I think many people will need these.
I'm not good in Obj-c thats why I cant send pull request, if you can convert this extension to obj-c than add it to your framework. Thank you for your support
It's works!!!
Thank you for your sharing, can I use these code to create a new project ? I think many people will need these.
If you asking about my extension, yes you can use it like your own code ☺️ but I no have idea about entire framework
I'm not good in Obj-c thats why I cant send pull request, if you can convert this extension to obj-c than add it to your framework. Thank you for your support
It's works!!!
Thank you for your sharing, can I use these code to create a new project ? I think many people will need these.
Dear, i am trying to get cropped image from imageview, but i can't get ... It is getting the entire image, if you have a good way, pleaes share your good experience with me. maybe other developers are looking the same result. thanks.
Please let me know can i get just cropped image without clear background as a full cropped image. Right now my image is cropping but returning the full image area with crop.
ped one.
I want it should come with full image part. image is centered aligned. Thank You.