How to reduce image in iOS, smoothing, but not soft?

I tried the popular UIImage + Resize category and with various interpolation settings. I tried scaling using the CG and CIFilters methods. However, I can never get a thumbnail that doesn't look slightly soft in focus and is not filled with jagged artifacts. Is there another solution or a third-party library that would allow me to get a very clear image?

This should be possible on the iPhone, because, for example, the Photos application will show a clear image, even when it is pinched, to reduce it.

+6
source share
4 answers

You told CG, but did not indicate your approach.

Using a drawing context or bitmap:

CGContextSetInterpolationQuality(gtx, kCGInterpolationHigh); CGContextSetShouldAntialias(gtx, true); << default varies by context type CGContextDrawImage(gtx, rect, image); 

and make sure your views and their layers do not resize the image again. I had good results. Other views may affect your view or context. If this doesn’t look good, try to check alone if something really distorts your view / image.

If you draw into a bitmap, you create a bitmap with the target dimensions, and then draw it.

Ideally, you will maintain aspect ratio.

Also note that this can be very processor intensive - drawing / scaling multiple times in HQ will cost a lot of time, so instead you can create a modified copy instead (using CGBitmapContext ).

+6
source

Here is the routine I wrote for this. A bit soft focus, although depending on how you scale the original image, it's not so bad. I am scaling software screenshots.

 - (UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize { UIGraphicsBeginImageContext(newSize); [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)]; UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return newImage; } 
+4
source

CGContextSetInterpolationQuality is what you are looking for.

You should try this add-on category http://vocaro.com/trevor/blog/2009/10/12/resize-a-uiimage-the-right-way/

0
source

When the image is reduced, it is often recommended to apply some sharpness.

The problem is that Core Image on iOS does not yet implement sharpening filters ( CISharpenLuminance , CIUnsharpMask ), so you have to minimize your own. Or nag Apple until they implement these filters on iOS.

However, Sharpen brightness and Unsharp mask are quite advanced filters, and in previous projects I found that even a simple 3x3 core will give clearly visible and satisfactory results.

Therefore, if you like to work at the pixel level, you can get image data from the graphics context, a bit to mask your path to the values ​​of R, G and B and code graphics, for example, in 1999. It will be a bit, like reinventing the wheel.

Maybe there are some standard graphics libraries that can do this too (ImageMagick?)

0
source

Source: https://habr.com/ru/post/907504/


All Articles