【问题标题】:CoreGraphics optimization核心图形优化
【发布时间】:2010-12-16 21:58:51
【问题描述】:

我的这段代码在 iPad 上运行得非常慢(出于某种原因,在 iPhone 上运行速度要快得多 ):

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

[self.navigationController setNavigationBarHidden:YES animated:NO];

mouseSwiped = YES;
UITouch *touch = [touches anyObject];   
currentPoint = [touch locationInView:frontgroundView];

CGContextBeginPath(context);
CGContextMoveToPoint(context, lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(context, currentPoint.x, currentPoint.y);
CGContextStrokePath(context);

maskRef = CGBitmapContextCreateImage(context);  

CGImageRef masked = CGImageCreateWithMask([backgroundImage CGImage], maskRef);
CGImageRelease(maskRef);

CGContextRef drawContext = UIGraphicsGetCurrentContext();

CGContextSaveGState(drawContext);
CGContextTranslateCTM (drawContext,0,frontgroundView.frame.size.height);
CGContextScaleCTM (drawContext, 1,-1);

CGContextDrawImage(drawContext, CGRectMake(0, 0, frontgroundView.frame.size.width, frontgroundView.frame.size.height), [frontgroundView.image CGImage]);

CGContextRestoreGState (drawContext);

CGContextDrawImage(drawContext, CGRectMake(0, 0, frontgroundView.frame.size.width, frontgroundView.frame.size.height), masked);

CGImageRef finalImage = CGBitmapContextCreateImage(drawContext);
frontgroundView.image = [UIImage imageWithCGImage:finalImage];
CGImageRelease(finalImage);

CGImageRelease(masked);

lastPoint = currentPoint;

mouseMoved++;

if (mouseMoved == 10)
    mouseMoved = 0;
}

有什么想法吗??

【问题讨论】:

  • 是的,使用 Instruments,或者至少告诉我们您的尝试。
  • 我建议使用 CoreAnimation 工具来查看需要很长时间的内容或提出更具体的问题。

标签: iphone cocoa-touch core-graphics quartz-graphics cgcontext


【解决方案1】:

您应该在 -drawRect: 中为您的视图进行绘图,而不是直接响应触摸。

【讨论】:

  • - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 方法中使用 ` [self setNeedsDisplay] ` 或 ` [self setNeedsLayout] `
猜你喜欢
  • 2012-05-12
  • 1970-01-01
  • 2012-02-16
  • 2011-10-05
  • 2015-09-21
  • 1970-01-01
  • 1970-01-01
  • 2012-07-09
相关资源
最近更新 更多