this article reprinted to http://adad184.com/2015/08/31/image-rotate-with-antialiasing/seen on Weibo on the weekends@ Zhou Xiwen KevinSpeaking of Calayer anti-aliasing problem
The specific approach is:
Think of a long time ago also encountered similar problems when it is necessary to do a class sticker application will naturally encounter the problem of sticker scaling and rotation, so the problem of sawtooth is also need to be solved, but at that time is the ios4,5 of the times there is no such allowsEdgeAntialiasing
thing (this thing iOS7 only public But IOS6 is also said to be able to use black technology. You know that)
So I turned to the almighty StackOverflow and got a very simple and not easy way to do it. Just leave a pixel transparent edge on the picture that needs to be shown .
Method
Method is relatively simple I wrote a uiimage category method and kept it in my tool store for several years (I just remembered it when I met this problem)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
- (UIImage *) AntiAlias { CGFloat border =1.0f; CGRect rect =CGRectMake (Border, border,Self. Size. width-2*border,Self. Size. height-2*border);
UIImage *img =Nil Uigraphicsbeginimagecontext (Cgsizemake (rect. Size. width,rect. Size. height)); [Self Drawinrect:CGRectMake (-1,-1,Self. Size.width, self.size IMG = uigraphicsgetimagefromcurrentimagecontext (); uigraphicsendimagecontext (); uigraphicsbeginimagecontext ( Self.size); [img Drawinrect:rect]; uiimage* antiimage = Uigraphicsgetimagefromcurrentimagecontext (); uigraphicsendimagecontext (); return antiimage; /span> |
First of all to see the actual effect (can not see clearly the students could point a large map)
You can see that if you do not do any processing at the time of rotation, there will be obvious aliasing, and the method of using transparent edges or setting allowsEdgeAntialiasing
can be anti-aliasing.
Next look at the performance comparison test method of the two methods is to rotate the 500 uiimageview on my iphone 5S and compare the number of frames with the following results
The results show that allowsEdgeAntialiasing
the performance is still more than the transparent edge of the way to be nearly so ...
- If your app is going to support ios6-you can refer to the transparent edge method The only drawback is that the display will be a little bit smaller than the original (my practice is to cut off the edge of 1px directly, of course you can also directly outside the image with 1px transparent edge)
- If your app only supports iOS7 recommend using allowsedgeantialiasing to set up simple and convenient if you feel the difficulty in performance, you can consider the method of transparent edge
Summary
The demo in this article can be found here
The method of testing may not be very rigorous (also the temporary spur wrote this article so did not spend too much time) if there is a mistake, please ask.
Anti-aliasing method for image distortion