iPhone4上的CoreGraphics速度比3G/3GS慢 [英] CoreGraphics slower on iPhone4 than on 3G/3GS

查看:118
本文介绍了iPhone4上的CoreGraphics速度比3G/3GS慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a chart drawn with CoreGraphics.

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

This chart can be scrolled horizontally and it's drawn as we scroll it.

问题在于,在3G/3GS上滚动的速度和性能都不错,但在iPhone 4上却慢于预期.

The problem is that on 3G/3GS the speed and performance of the scroll is good but on iPhone 4 is slower than expected.

我认为这是与iPhone 4的更高分辨率有关的问题.这是正确的吗?

I suppose this is a issue related to the higher resolution of the iPhone 4. Is that correct?

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

How can I increase the performance on iPhone 4? Does the framework automatic conversion to draw on iPhone 4 resolution or is it my work?

非常感谢您.

推荐答案

在处理CoreGraphics时,iPhone 4的运行速度可能会慢很多.

The iPhone 4 can be much slower when it comes to CoreGraphics processing.

您的情况可能有一个主意.当用户滚动而不是绘制完整分辨率时,请向您的CoreGraphics相关内容绘制一个半分辨率上下文.然后将该上下文用作位图,并将其放大到完整分辨率.这仅适用于iPhone4,您将失去更好的高分辨率品质,但仅限于用户滚动时.

One possible idea for your case. When the user is scrolling, rather than draw full resolution, draw your CoreGraphics related stuff a half resolution context. Then take that context as a bitmap and scale it up to full resolution. This would be for iPhone4 only and you would lose the nice higher resolution quality, but only be while the user is scrolling.

这是我编写的一些代码,用于在不同设备之间进行测试.

Here's some code I wrote up for testing between the different devices.

全屏显示视图时的结果.

My results when displaying the view at full screen.

  • 3G, 9 FPS
  • 3GS, 16 FPS
  • i4, 5 FPS (打击乐器)
  • 3G, 9 FPS
  • 3GS, 16 FPS
  • i4, 5 FPS (bummer)

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

这篇关于iPhone4上的CoreGraphics速度比3G/3GS慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆