Anti-aliasing processing method for image deformation

Anti-aliasing processing method for image deformation

[[148963]]

Preface

I saw @周楷雯Kevin talking about CALayer anti-aliasing issues on Weibo over the weekend

The specific steps are:

  1. layer.allowsEdgeAntialiasing = true  

I remembered that I encountered a similar problem a long time ago. At that time, I had to make a sticker-like application, so of course I would encounter the problem of sticker scaling and rotation, so the aliasing problem also needed to be solved. But at that time, it was the era of iOS 4 and 5, and there was no such thing as allowsEdgeAntialiasing mentioned above (this thing was not released until iOS 7, but it is said that iOS 6 can also be used, but it is black technology... you know)

So I asked for help from stackoverflow and got a very simple but not simple method, which is what I said on Weibo. Just leave a one-pixel transparent border on the image to be displayed and it will be done.

method

The method is relatively simple. I wrote it as a UIImage Category method and it has been sitting in my tool library for years (I only remembered it when I encountered this problem).

  1. - (UIImage *)antiAlias
  2. {
  3. CGFloat border = 1.0f ;
  4. CGRect rect = CGRectMake(border, border, self.size.width- 2 *border, self.size.height- 2 *border);
  5. UIImage *img = nil;
  6.      
  7. UIGraphicsBeginImageContext(CGSizeMake(rect.size.width,rect.size.height));
  8. [self drawInRect:CGRectMake(- 1 , - 1 , self.size.width, self.size.height)];
  9. img = UIGraphicsGetImageFromCurrentImageContext();
  10. UIGraphicsEndImageContext();
  11.      
  12. UIGraphicsBeginImageContext(self.size);
  13. [img drawInRect:rect];
  14. UIImage* antiImage = UIGraphicsGetImageFromCurrentImageContext();
  15. UIGraphicsEndImageContext();
  16.      
  17. return antiImage;
  18. }

Let’s take a look at the actual effect first

It can be seen that if no processing is done during rotation, there will be obvious aliasing. Using the transparent edge method or setting allowsEdgeAntialiasing can eliminate aliasing.

Next, let’s take a look at the performance comparison of these two methods. The test method is to rotate 500 UIImageViews on my iPhone 5S and compare the frame rates. The results are as follows

The results show that the performance of allowsEdgeAntialiasing is still slightly worse than the transparent edge method, so...

  • If your application needs to support iOS6- you can refer to the transparent border method. The only disadvantage is that the image will be a little smaller than the original one (I cut off the 1px border directly. Of course, you can also add a 1px transparent border directly outside the image)

  • If your app only supports iOS7, it is recommended to use allowsEdgeAntialiasing for easy and convenient settings. If you feel that the performance is difficult, you can consider the transparent edge method.

summary

The demo in this article can be found here

The testing method may not be very rigorous (I wrote this article on the spur of the moment so it didn't take me too much time). If there are any errors, please point them out.

<<:  Summary of tips for Android code optimization

>>:  App Store exposure data and acquisition methods

Recommend

Why Apple abandoned Helvetica - revealing the secret of iOS9's ugly fonts

Since the first iPhone, Apple has used Helvetica ...

Why spend the money? The iPad's built-in "Notes" is so useful

As expected, WWDC upgraded five major systems. Un...

Why can't we feel the earth spinning?

Regarding rotation, it's not that simple. By ...

Summarize some suggestions from Effective Java that can help Android development

Effective Java is considered by many to be one of...

Practice: Full process analysis to improve APP Push conversion rate

Recently, we are working on optimizing the succes...

New Android vulnerability exposed: secretly taking photos and uploading

Following the multiple vulnerabilities in iOS7.1.1...

How to build an operation system?

When I wrote this title, I actually felt a little...

Creating JavaScript modules with Babel and ES7

Last year, a new version of JavaScript was releas...

Writing Scripts in Swift: Git Hooks

Preface This week, I decided to improve my Git wo...

Facial recognition has become a reality. Have you been recognized?

Do you still remember these plots in the movies? ...

On the winter solstice, who can resist a bowl of hot dumplings?

The Winter Solstice is not only an important sola...

Zeng Shen's exquisite ni product performance first issue

Zeng Shen's exquisite ni product performance ...