I fetch an image from the web and show it in an UIImage, but first i scaled it down. The thing is, if the image is black and white, the result is pretty bad, with lots of white lines crossing the pic.
I've tried many variations but i see no improvement, always the same poor quality result. Any tips?
Some of the things i tried (remember i'm working without an 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
Turns out @cbranch was right, so first here is the solution i found (without releases):
- (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;
}
Notice that this isn't necessary for iOS5, don't know why (nothing about this in the documentation).