UIImage always become indistinct when it was scaled.What can i do if make it keep clearness?
- (UIImage *)rescaleImageToSize:(CGSize)size {
CGRect rect = CGRectMake(0.0, 0.0, size.width, size.height);
UIGraphicsBeginImageContext(rect.size);
[self drawInRect:rect]; // scales image to rect
UIImage *resImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return resImage;
}
Rounding
First, make sure that you're rounding your size before scaling.
drawInRect:
can blur an otherwise usable image in this case. To round to the nearest integer value:For certain tasks, you may want to round down (floorf) or round up (ceilf) instead.
CILanczosScaleTransform not available
Then, disregard my previous recommendation of CILanczosScaleTransform. While parts of Core Image are available in iOS 5.0, Lanczos scaling is not. If it ever does become available, make use of it. For people working on Mac OS, it is available, use it.
vImage Scaling
However, there is a high-quality scaling algorithm available in vImage. The following pictures show how a method using it (vImageScaledImage) compares with the different context interpolation options. Also note how those options behave differently at different zoom levels.
On this diagram, it preserved the most line detail:
On this photograph, compare the leaves at lower left:
On this photograph, compare the textures in lower right:
Do not use it on pixel art; it creates odd scaling artifacts:
Although it on some images it has interesting rounding effects:
Performance
Not surprisingly, kCGImageInterpolationHigh is the slowest standard image interpolation option. vImageScaledImage, as implemented here, is slower still. For shrinking the fractal image to half its original size, it took 110% of the time of UIImageInterpolationHigh. For shrinking to a quarter, it took 340% of the time.
You may think otherwise if you run it in the simulator; there, it can be much faster than kCGImageInterpolationHigh. Presumably the vImage multi-core optimisations give it a relative edge on the desktop.
Code