3

我有一个用 CoreGraphics 绘制的图表。

该图表可以水平滚动,并在我们滚动时绘制。

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

我想这是与 iPhone 4 的高分辨率有关的问题。对吗?

如何提高 iPhone 4 的性能?框架会自动转换以绘制 iPhone 4 分辨率还是我的工作?

非常感谢你。

4

2 回答 2

6

iPhone 4在处理 CoreGraphics 时可能要慢得多

您的情况的一种可能的想法。当用户滚动时,而不是绘制全分辨率,绘制与 CoreGraphics 相关的东西半分辨率上下文。然后将该上下文作为位图并将其放大到全分辨率。这仅适用于 iPhone4,您将失去更好的高分辨率质量,但仅限于用户滚动时。

这是我为在不同设备之间进行测试而编写的一些代码。

全屏显示视图时的结果。

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

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
于 2010-09-08T17:19:16.117 回答
0

“它更慢”的体验,你能更准确地描述它吗?我假设滚动的帧速率已经降低到看起来不流畅甚至卡顿的地方。

如果是这样,并且如果您确定解决方案是导致此问题的原因,那么我可以看到两种通用方法。

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

我假设您已经查看了视图的“绘图”属性、不透明等,因为它们确实会影响性能。

于 2010-09-03T08:22:11.123 回答