iPhone4上的CoreGraphics比3G/3GS慢

eme*_*gro 3 iphone core-graphics iphone-4

我有一张用CoreGraphics绘制的图表.

此图表可以水平滚动,并在滚动时绘制.

问题是在3G/3GS上滚动的速度和性能都很好,但在iPhone 4上的速度比预期慢.

我想这是一个与iPhone 4更高分辨率相关的问题.这是正确的吗?

如何提高iPhone 4的性能?框架是否自动转换为在iPhone 4分辨率上绘制,还是我的工作?

非常感谢你.

tid*_*all 6

在CoreGraphics处理方面,iPhone 4可能慢得多.

一个可能的想法你的情况.当用户滚动而不是绘制完整分辨率时,将CoreGraphics相关内容绘制为半分辨率上下文.然后将该上下文作为位图并将其扩展到完整分辨率.这仅适用于iPhone4,您将失去更好的更高分辨率质量,但仅限用户滚动时.

这是我为不同设备之间的测试编写的一些代码.

在全屏显示视图时的结果.

  • 3G,9 FPS
  • 3GS,16 FPS
  • i4,5 FPS(无赖)

CoreGraphicsTestView.h

#import <UIKit/UIKit.h>
@interface CoreGraphicsTestView : UIView 
{
    int displayCounter;
    float displayInterval;
    char fps[50];
}
@end
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)