代码之家  ›  专栏  ›  技术社区  ›  emenegro

iPhone4上的CoreGraphics比3G/3GS上的慢

  •  3
  • emenegro  · 技术社区  · 15 年前

    这个图表可以水平滚动,它是我们滚动时绘制的。

    问题是,在3G/3GS上,滚动条的速度和性能都不错,但在iphone4上却比预期慢。

    如何提高iPhone4的性能?这个框架是自动转换到iPhone4分辨率还是我的工作?

    非常感谢你。

    2 回复  |  直到 15 年前
        1
  •  6
  •   tidwall    15 年前

    iPhone4可以 许多的 核心图形处理速度较慢。

    你的案子有一个可能的想法。当用户滚动时,不要绘制完全分辨率,而是将CoreGraphics相关的内容绘制为半分辨率上下文。然后将该上下文作为位图并将其放大到完全分辨率。这将是iPhone4只和你会失去不错的更高的分辨率质量,但只有当用户滚动。

    全屏显示视图时的结果。

    • 每秒9帧
    • 3克,
    • i4号, (郁闷)

    CoreGraphicsTestView.h

    #import <UIKit/UIKit.h>
    @interface CoreGraphicsTestView : UIView 
    {
        int displayCounter;
        float displayInterval;
        char fps[50];
    }
    @end
    

    CoreGraphicsTestView.m

    #import "CoreGraphicsTestView.h"
    #define RNDN(s, e) ((((CGFloat)rand() / (CGFloat)INT_MAX) * ((e)-(s)) + (s)))
    #define RNDX(s, e, r) (RNDN((s), (e)) * (r).size.width)
    #define RNDY(s, e, r) (RNDN((s), (e)) * (r).size.height)
    #define RNDR(r) (CGRectMake(RNDX(0,0.50,(r)),RNDY(0,0.50,(r)),RNDX(0.50,1.0,(r)),RNDY(0.50,1.0,(r))))
    @implementation CoreGraphicsTestView
    - (id)initWithFrame:(CGRect)frame 
    {
        if ((self = [super initWithFrame:frame])) 
        {
            self.backgroundColor = [UIColor blackColor];
            srand(time(NULL));
            fps[0] = '\0';
        }
        return self;
    }
    - (void)updateDisplayCounter:(CGContextRef)c rect:(CGRect)rect
    {
        displayCounter++;
        float now = (float)clock() / (float)CLOCKS_PER_SEC;
        if (now - displayInterval > 1)
        {
            sprintf(fps, "%0.0f", displayCounter / (now - displayInterval));
            printf("%s\n", fps);
            displayInterval = now;
            displayCounter = 0;
        }
        CGContextTranslateCTM(c, 5, 40);
        CGContextScaleCTM(c, 1.0, -1.0); 
        CGContextSetFillColorWithColor(c, [UIColor whiteColor].CGColor);
        CGContextSelectFont(c, "Arial", 18, kCGEncodingMacRoman);
        CGContextShowTextAtPoint(c, 0, 0, fps, strlen(fps));
    }
    - (void)setRandomColor:(CGContextRef)c
    {
        CGFloat components[4] = {1,RNDN(0, 1),RNDN(0, 1),RNDN(0, 1)};
        CGContextSetFillColor(c, components);
    }
    - (void)drawRect:(CGRect)rect
    {
        CGContextRef c = UIGraphicsGetCurrentContext(); 
        for (int i=0;i<5;i++)
        {
            [self setRandomColor:c];
            CGContextFillRect(c, RNDR(rect));
            [self setRandomColor:c];
            CGContextFillEllipseInRect(c, RNDR(rect));
        }
        [self updateDisplayCounter:c rect:rect];
        [self performSelector:@selector(setNeedsDisplay) withObject:nil afterDelay:0.0];
    }
    @end
    
        2
  •  0
  •   Henrik Erlandsson    15 年前

    如果是这样的话,如果你确定这是决议的原因,我可以看到两种通用的方法。

    • 例如,以一半的间隔进行更新,将一半渲染为宽切片。如果Cocoa Touch不能促进这一点,您可以从现在的呈现方法中以一定的时间间隔触发回调。
    • 渲染“进一步提前”,以便在滚动到视图时渲染隐藏在可视区域之外的图形的一部分。

    推荐文章