2011-10-30 68 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的,不知道爲什麼(在文件中關於這個沒什麼)。