2011-10-30 69 views
1

我从网上获取图像并在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

+0

你试过什么样的变化? –

+0

向我们展示一些代码。 – zaph

+2

假设您的图像是每像素1位,您是否能够在缩放之前将其转换为灰度(8 BPP或更好)? – cbranch

回答

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的,不知道为什么(在文件中关于这个没什么)。