CoreGraphics is slower on iPhone4 than on 3G / 3GS

I have a chart drawn using CoreGraphics.

This chart can be scrolled horizontally and drawn when we scroll it.

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

I believe this is a problem with the higher resolution of the iPhone 4. Is this correct?

How to increase productivity on iPhone 4? Is it automatically converting a drawing frame to iPhone 4 or is it my job?

Thank you very much.

+3
source share
2 answers

iPhone 4 can be a lot slower when it comes to handling CoreGraphics.

One possible idea for your business. When the user scrolls through rather than displaying the full resolution, draw CoreGraphics-related material in a half-resolution context. Then take this context as a bitmap and enlarge it to full resolution. This will only be for iPhone4 and you will lose good quality with a higher resolution, but only while scrolling the user.

Here is the code I wrote to test between different devices.

My results when displaying a view in full screen.

  • 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 
+6
source

Experience "it is slower", could you describe it more accurately? I assume that the frame rate of the scroll is reduced to the point where it does not look smooth or even stutters.

If this is the case, and if you are sure that this is the reason that is causing this, there are two general methods that I can see.

  • refresh, for example, half the interval to get half the width of the slices. If Cocoa Touch doesnโ€™t make it easy, you can call the time-interval callback from the rendering method you have.
  • render โ€œforwardโ€, so that the part of the chart hidden further beyond the visible area is displayed when scrolling in the view.

I assume that you have already looked at the "Drawing" properties of the view, Opaque, etc., since they affect performance.

0
source

Source: https://habr.com/ru/post/1468985/


All Articles