缩放黑白图像

时间:2011-10-30 21:08:27

标签: objective-c ios core-graphics

我从网上获取图片并在UIImage中显示,但首先我将其缩小。问题是,如果图像是黑白的,结果非常糟糕,有很多白线穿过图片。

我尝试了很多变化,但我看不到任何改进,总是同样质量差的结果。有什么提示吗?

我试过的一些事情(记得我在没有UIImageView的情况下工作):

http://iosdevelopertips.com/graphics/how-to-scale-an-image-using-an-objective-c-category.html

How to scale a UIImageView proportionally?

What's the easiest way to resize/optimize an image size with the iPhone SDK?

http://www.iphonedevsdk.com/forum/iphone-sdk-development/5204-resize-image-high-quality.html

1 个答案:

答案 0 :(得分:2)

结果证明@cbranch是对的,所以首先是我发现的解决方案(没有发布):

- (BOOL) isGrayscaleImage:(UIImage *)image
{
    CGImageRef imgRef = [image CGImage];
    CGColorSpaceModel clrMod = CGColorSpaceGetModel(CGImageGetColorSpace(imgRef));

    switch (clrMod) {
        case kCGColorSpaceModelMonochrome : 
            return YES;
        default:
            return NO;
    }
}

- (UIImage *) imageToGrayscale:(UIImage *)image
{
    CGSize size = image.size; 
    CGRect rect = CGRectMake(0.0f, 0.0f, size.width, size.height); 
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray(); 
    CGContextRef context = CGBitmapContextCreate(nil, size.width, size.height, 8, 0, colorSpace, kCGImageAlphaNone); 
    CGColorSpaceRelease(colorSpace); 
    CGContextDrawImage(context, rect, [image CGImage]); 
    CGImageRef grayscale = CGBitmapContextCreateImage(context);
    UIImage *img = [UIImage imageWithCGImage:grayscale];
    return img;
}

请注意,iOS5不需要这样做,不知道为什么(文档中没有这个)。