문제

I'm working on a project that involves users interacting with straight lines rendered in Core Graphics, on top of a UIImage. Each end of the line has a "handle" for the user to interact with. When dragged, setNeedsDisplay is called and the line is re-drawn since the length and angle change.

I'm using the following code to redraw the line. It's pretty straightforward, but I'm starting think that Core Graphics might not be the right approach to this.

The following is called by drawRect:

- (void)drawLines {
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextSaveGState(ctx);

    CGContextBeginPath(ctx);
    CGContextSetStrokeColorWithColor(ctx, [[UIColor blueColor] CGColor]);
    CGContextSetLineWidth(ctx, 6.0);
    CGContextSetAlpha(ctx, 1.0);
    CGContextMoveToPoint(ctx, pointA.x, pointA.y);
    CGContextAddLineToPoint(ctx, pointB.x, pointB.y);
    CGContextStrokePath(ctx);

    CGContextRestoreGState(ctx);
    }

EDIT: The problem that I'm facing is that when I drag this item around quickly, memory usage spikes, I get a memory warning, and then it crashes. Xcode says "Terminated due to memory pressure."

Thanks in advance for your thoughts!

도움이 되었습니까?

해결책

Solved. The size of the UIView was just too big. Calling setNeedsDisplay on such a huge UIView WILL cause a memory spike and crash. Apple used to limit UIView to 1024x1024. While this is no longer the limit, they still recommend keeping the size down to keeps memory footprint low.

source: https://developer.apple.com/library/ios/documentation/uikit/reference/uiview_class/uiview/uiview.html

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top