2
我无法找到一种方法来裁剪包含白色边框的图像。修剪UIImage白色边框
目前,它是一个UIImage的,我试图像UIImage+Trim和CKImageAddons一些解决方案,但这些库没有工作。我认为部分问题在于由于图像压缩(我认为),边框不完全是白色的。
有没有人找到解决方案?
感谢,
我无法找到一种方法来裁剪包含白色边框的图像。修剪UIImage白色边框
目前,它是一个UIImage的,我试图像UIImage+Trim和CKImageAddons一些解决方案,但这些库没有工作。我认为部分问题在于由于图像压缩(我认为),边框不完全是白色的。
有没有人找到解决方案?
感谢,
嗯,我会使用的UIImage +微调,改变它的工作原理来算可见白色像素的方式(因此如果RGB> 255 - small_number_that_still_makes_it_white_but_suffices_to_cut_like_5_to_10)insted的计算不透明的。
后快速浏览一下,计数循环是在这里:
for (NSInteger row = 0; row < height; row++) {
for (NSInteger col = 0; col < width; col++) {
if (fullyOpaque) {
// Found non-transparent pixel
if (bitmapData[row*bytesPerRow + col] == UINT8_MAX) {
rowSum[row]++;
colSum[col]++;
}
} else {
// Found non-transparent pixel
if (bitmapData[row*bytesPerRow + col]) {
rowSum[row]++;
colSum[col]++;
}
}
}
}
和切割在后面做,并删除rowSum和colSum是等于0
而且,似乎它创建内部图像一个用于计数:
CGContextRef contextRef = CGBitmapContextCreate(bitmapData, (NSUInteger)width, (NSUInteger)height, 8, (NSUInteger)bytesPerRow, NULL, kCGImageAlphaOnly);
使用灰度图像,而不是应该工作的简单的方法,它从测试的透明度改变颜色测试。
编辑:好的,解决方案:
- (UIEdgeInsets)transparencyInsetsByCuttingWhitespace:(UInt8)tolerance
{
// Draw our image on that context
NSInteger width = (NSInteger)CGImageGetWidth([self CGImage]);
NSInteger height = (NSInteger)CGImageGetHeight([self CGImage]);
NSInteger bytesPerRow = width * (NSInteger)sizeof(uint8_t);
// Allocate array to hold alpha channel
uint8_t * bitmapData = calloc((size_t)(width * height), sizeof(uint8_t));
// Create grayscale image
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
CGContextRef contextRef = CGBitmapContextCreate(bitmapData, (NSUInteger)width, (NSUInteger)height, 8, (NSUInteger)bytesPerRow, colorSpace, kCGImageAlphaNone);
CGImageRef cgImage = self.CGImage;
CGRect rect = CGRectMake(0, 0, width, height);
CGContextDrawImage(contextRef, rect, cgImage);
// Sum all non-transparent pixels in every row and every column
uint16_t * rowSum = calloc((size_t)height, sizeof(uint16_t));
uint16_t * colSum = calloc((size_t)width, sizeof(uint16_t));
// Enumerate through all pixels
for (NSInteger row = 0; row < height; row++) {
for (NSInteger col = 0; col < width; col++) {
// Found darker pixel
if (bitmapData[row*bytesPerRow + col] <= UINT8_MAX - tolerance) {
rowSum[row]++;
colSum[col]++;
}
}
}
// Initialize crop insets and enumerate cols/rows arrays until we find non-empty columns or row
UIEdgeInsets crop = UIEdgeInsetsZero;
// Top
for (NSInteger i = 0; i < height; i++) {
if (rowSum[i] > 0) {
crop.top = i;
break;
}
}
// Bottom
for (NSInteger i = height - 1; i >= 0; i--) {
if (rowSum[i] > 0) {
crop.bottom = MAX(0, height - i - 1);
break;
}
}
// Left
for (NSInteger i = 0; i < width; i++) {
if (colSum[i] > 0) {
crop.left = i;
break;
}
}
// Right
for (NSInteger i = width - 1; i >= 0; i--) {
if (colSum[i] > 0) {
crop.right = MAX(0, width - i - 1);
break;
}
}
free(bitmapData);
free(colSum);
free(rowSum);
CGContextRelease(contextRef);
return crop;
}
- (UIImage *)imageByTrimmingWhitePixelsWithOpacity:(UInt8)tolerance
{
if (self.size.height < 2 || self.size.width < 2) {
return self;
}
CGRect rect = CGRectMake(0, 0, self.size.width * self.scale, self.size.height * self.scale);
UIEdgeInsets crop = [self transparencyInsetsByCuttingWhitespace:tolerance];
UIImage *img = self;
if (crop.top == 0 && crop.bottom == 0 && crop.left == 0 && crop.right == 0) {
// No cropping needed
} else {
// Calculate new crop bounds
rect.origin.x += crop.left;
rect.origin.y += crop.top;
rect.size.width -= crop.left + crop.right;
rect.size.height -= crop.top + crop.bottom;
// Crop it
CGImageRef newImage = CGImageCreateWithImageInRect([self CGImage], rect);
// Convert back to UIImage
img = [UIImage imageWithCGImage:newImage scale:self.scale orientation:self.imageOrientation];
CGImageRelease(newImage);
}
return img;
}
嘿Rychu,谢谢!我会尽快尝试,让你知道它是如何发展的。 –
好吧,当我得到片刻时,我会尝试一下 – Rychu
嘿!试过了,它工作得很好。但有时它会收获云朵!例如与这张照片:http://distilleryimage4.s3.amazonaws.com/cdfe16d45f5e11e3adc2123587d614a2_6.jpg –