简体   繁体   English

- (void)drawRect:(CGRect)rect; 正在耗尽几乎所有的iPhone CPU

[英]-(void) drawRect:(CGRect)rect; is using up nearly all of iPhone CPU

- (void)drawRect:(CGRect)rect{

    float sliceSize = rect.size.width / imagesShownAtOnce;
    //Apply our clipping region and fill it with black
    [clippingRegion addClip];
    [clippingRegion fill];

    //Draw the 3 images (+1 for inbetween), with our scroll amount.
    CGPoint loc;
    for (int i=0;i<imagesShownAtOnce+1;i++){
        loc = CGPointMake(rect.origin.x+(i*sliceSize)-imageScroll, rect.origin.y);
        [[buttonImages objectAtIndex:i] drawAtPoint:loc];
    }

    //Draw the text region background
    [[UIColor blackColor] setFill];
    [textRegion fillWithBlendMode:kCGBlendModeNormal alpha:0.4f];

     //Draw the actual text.
    CGRect textRectangle = CGRectMake(rect.origin.x+16,rect.origin.y+rect.size.height*4/5.6,rect.size.width/1.5,rect.size.height/3);
    [[UIColor whiteColor] setFill];
    [buttonText drawInRect:textRectangle withFont:[UIFont fontWithName:@"Avenir-HeavyOblique" size:22]];
}

clippingRegion and textRegion are UIBezierPaths to give me the rounded rectangles I want (First for a clipping region, 2nd as an overlay for my text) clippingRegiontextRegionUIBezierPaths ,为我提供了我想要的圆角矩形(首先是剪裁区域,第二个是我文本的叠加)

The middle section is drawing 3 images and letting them scroll along, which im updating every 2 refreshes from a CADisplayLink , and that invalidates the draw region by calling [self setNeedsDisplay] , and also increasing my imageScroll variable. 中间部分绘制了3个图像并让它们滚动,我每次刷新CADisplayLink都会更新,并通过调用[self setNeedsDisplay]使绘图区域无效,并且还增加了我的imageScroll变量。


Now that that background information is done, here is my issue: 现在该背景信息已经完成,这是我的问题:

It runs, and even runs smoothly. 它运行,甚至运行顺利。 But it is using up an absolutely high amount of CPU time (80%+)!! 但它占用了绝对高的CPU时间(80%+)!! How do I push this off to the GPU on the phone instead? 如何将其关闭到手机上的GPU? Someone told me about CALayers but I've never dealt with them before 有人告诉我关于CALayers但我以前从未处理过他们

Draw each component of your drawing once into something (a view or layer) and let it hold the cached the drawing. 将绘图的每个组件绘制成某个(视图或图层)并让它保持缓存绘图。 Then you just move or transform each component, and exactly as you say, it's all done by the GPU. 然后你只需移动或转换每个组件,就像你说的那样,它都是由GPU完成的。

You could do this with individual views or with individual layers, but that doesn't really matter (a view is a layer, under the hood). 您可以使用单个视图或单个图层执行此操作,但这并不重要(视图一个层,在引擎盖下)。 The point is that there is no need to be constantly redrawing from scratch when all you really want is to move the same persistent pieces around. 关键在于,当您真正想要的是移动相同的持久碎片时,不需要从头开始不断地重绘。

Learning about CALayer would be a good idea, as it is in fact the basis of all drawing on iOS. 了解CALayer将是一个好主意,因为它实际上是iOS上所有绘图的基础。 What could be more important to know about than that? 有什么比这更重要的了解?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何调用 -(void)drawRect:(CGRect)rect - How to call -(void)drawRect:(CGRect)rect (void)drawRect:(CGRect)rect iOS 5.1 OpenGL ES 2.0中的错误 - Errors in (void)drawRect:(CGRect)rect iOS 5.1 OpenGL ES 2.0 -(void)drawRect:(CGRect)rect,绘制一个迷宫并检测其边界 - - (void)drawRect:(CGRect)rect , draw a maze and detect its boundary 将阴影放置到矩形中(drawRect:(CGRect)rect) - Put shadow to a rectangle (drawRect:(CGRect)rect) 在drawRect:(CGrect)rect,objective中不显示阴影 - shadow does not show in drawRect:(CGrect)rect,objective UITextField - (void)drawPlaceholderInRect:(CGRect)rect在iOS 7中返回不同的CGRect高度 - UITextField - (void)drawPlaceholderInRect:(CGRect)rect returns different CGRect height in iOS 7 覆盖drawRect:(CGRect)rect在Objective-C(iOS)中吗? - Overriding drawRect:(CGRect)rect in Objective-C (iOS)? 在自定义UIView中为rectRect方法使用rect参数 - using rect argument for drawRect method in custom UIView 怎么做 - (CGRect)convertRect:(CGRect)rect toView:(UIView *)查看工作 - How does - (CGRect)convertRect:(CGRect)rect toView:(UIView *)view work - (CGRect)convertRect:(CGRect)rect toView:(nullable UIView *)view - - (CGRect)convertRect:(CGRect)rect toView:(nullable UIView *)view
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM