I have problem with my app. It is developed for both devices. On iPad it works great, but if I run it on iPhone it sucks. It slows down a lot. Note:I use storyboards and same View controllers and delegates for both devices.
Any suggestions?
It is iOS 5, On iPhone 3, 3S, 4, and 4S. Everyone is experiencing problems with this.
It draws in response to finger movement.
If I touch the screen, the line I draw falls behind….. and I don’t know why.
On iPad it works great .
maybe but main there could be problem here
- (void)drawRect:(CGRect)rect
{
CGImageRef cgImage = CGBitmapContextCreateImage(offScreenBuffer);
UIImage *uiImage = [[UIImage alloc] initWithCGImage:cgImage];
CGImageRelease(cgImage);
[uiImage drawInRect:self.bounds];
[uiImage release];
}
this method is called from touches events …
-(void)drawPoint:(UITouch *)touch {
currentLoc = [[PointLocation alloc] init];
currentLoc.location = [touch locationInView:self];
self.previousPoint = self.point;
self.point = currentLoc;
[self drawToBuffer];
[currentLoc release];
}
and this is draw to buffer….
-(void)drawToBuffer {
CGFloat color[4] = {R,G,B,A};
if (self.previousPoint != nil) {
CGContextSetRGBStrokeColor(offScreenBuffer, color[0],color[1],color[2],color[3]);
CGContextBeginPath(offScreenBuffer);
CGContextSetLineWidth(offScreenBuffer, lane);
CGContextSetLineCap(offScreenBuffer, kCGLineCapRound);
CGContextMoveToPoint(offScreenBuffer, previousPoint.location.x, previousPoint.location.y);
CGContextAddLineToPoint(offScreenBuffer, point.location.x, point.location.y);
CGContextDrawPath(offScreenBuffer, kCGPathStroke);
}
[self setNeedsDisplay];
}
but now i really don’t know and have no idea…:(
creating offscreenbuffer
-(CGContextRef)setupBuffer {
CGSize size = self.bounds.size;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(NULL, size.width, size.height, 8, size.width*4, colorSpace, kCGImageAlphaPremultipliedLast);
CGColorSpaceRelease(colorSpace);
CGContextTranslateCTM(context, 0, size.height);
CGContextScaleCTM(context, 1.0, -1.0);
return context;
}
You can cut a few corners by drawing the
CGImagedirectly, without theUIImagestep, usingthis will probably flip the image vertically, but that can be fixed. It may or may not make a difference, I don’t know what overhead
UIImageadds.As for strange performance differences, you may want to look into the memory alignment of the underlying bitmap. (see the comments for my question regarding
offscreenBuffer)update regarding the code shown in the updated question
The code is not really efficient. For every touch event, you have this happening:
-drawPoint:PointLocation, assignment using setter (self.point = ...)-drawToBufferthese may each not take a lot of time, but added together it might just make a difference if you would reduce the complexity. Per item you can try the following:
lastPoint = currPoint; currPoint = point;, with the rest of the code adapted of courseCGContextSaveGState(offscreenBuffer)andCGContextRestoreGState(offscreenBuffer)to see if it is fasterthere is a neat function for just drawing a simple line:
CGContextStrokeLineSegments, which may be faster. Use it like this:CGPoint points[2] = { lastPoint, currPoint };CGContextStrokeLineSegments(offscreenBuffer,points,2);profile/profile/profile: test to see if changes make things faster, by measuring rendering time. At the top:
NSDate *tstart = [NSDate date];and at the bottomNSLog(@"drawRect took %f ms",-[tstart timeIntervalSinceNow]*1000);You can tackle each of these points individually and check if it still works.
Good luck!