Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use CoreGraphics to realize a simple drawing application

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Today, I will talk to you about how to use CoreGraphics to achieve a simple drawing application, many people may not understand, in order to let you better understand, the editor summed up the following content, I hope you can get something according to this article.

Sometimes it is not so feasible to replace CoreGraphics with CAShapeLayer or other vector graphics layers. For example, our drawing application: we use lines to complete the vector drawing perfectly. But imagine if we could further improve the performance of the application, make it work like a blackboard, and then draw lines with "chalk". The easiest way to simulate chalk is to use a "line brush" image and paste it where the user's finger touches it, but this method cannot be done with CAShapeLayer.

How to use CoreGraphics to realize a simple drawing application

We can create a separate layer for each "line brush", but there is a big problem with its implementation. The number of layers allowed to appear on the screen at the same time is about a few hundred, so we will soon exceed it. There's nothing we can do in this case, just use CoreGraphics (unless you want to do something more complicated with OpenGL).

In the initial implementation of our "blackboard" application, we changed the previous version of DrawingView to replace UIBezierPath with an array of brush positions.

Frame rate and line quality will decline over time

In order to reduce unnecessary drawing, MacOS and iOS devices will divide the screen into areas that need to be redrawn and areas that do not need to be redrawn. Those parts that need to be redrawn are called "dirty areas". In practical application, in view of the complexity of boundary clipping and blending of non-rectangular regions, the rectangular position containing the specified view is usually distinguished, and this position is the "dirty rectangle".

When a view has been changed, TA may need to be redrawn. But in many cases, only part of the view has been changed, so it would be wasteful to redraw the entire boarding map. But CoreAnimation usually doesn't know your custom drawing code, and it can't calculate the location of dirty areas on its own. However, you can provide this information.

When you detect that a specified part of a specified view or layer needs to be redrawn, you directly call-setNeedsDisplayInRect: to mark it, and then pass in the affected rectangle as a parameter. This invokes the view's-drawRect: (or layer proxy's-drawLayer:inContext: method) when a view refresh occurs.

The CGContext parameter passed in-drawLayer:inContext: is automatically trimmed to fit the corresponding rectangle. To determine the size of the rectangle, you can use the CGContextGetClipBoundingBox () method to get the size from the context. It is easier to call-drawRect () because CGRect is passed directly as an argument.

You should limit your drawing work to this rectangle. Any drawing outside this area will be automatically ignored, but the time spent on computing and abandoning CPU is wasted, which is not worth it.

Instead of relying on CoreGraphics to redraw for you, cropping out your own drawing area may allow you to avoid unnecessary operations. That is, if your tailoring logic is complex, let CoreGraphics do it for you, and remember: do it only when you can do it efficiently.

After reading the above, do you have any further understanding of how to implement a simple drawing application with CoreGraphics? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report