菜鸟教程小白 发表于 2022-12-12 12:35:08

iOS 绘画优化


                                            <p><p>您好,我目前正在开发一个包含通过绘图记笔记的应用程序。我遵循了 ray wenderlich 教程,就我所知,我最终得到了以下代码:</p>

<pre><code>- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    mouseSwiped = NO;
    UITouch *touch = ;
    lastPoint = ;
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    CGFloat red,green,blue,alpha;

    mouseSwiped = YES;
    UITouch *touch = ;
    CGPoint currentPoint = ;



    UIGraphicsBeginImageContext(self.mainImage.frame.size);
    ;

    CGContextSetBlendMode(UIGraphicsGetCurrentContext(),);

    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x , lastPoint.y);
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x , currentPoint.y);
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), );

    [ getRed:&amp;red green:&amp;green blue:&amp;blue alpha:&amp;alpha];
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red, green, blue, 1);

    CGContextSetBlendMode(UIGraphicsGetCurrentContext(),);

    CGContextStrokePath(UIGraphicsGetCurrentContext());
    self.tempDrawImage.image = UIGraphicsGetImageFromCurrentImageContext();
    ];
    UIGraphicsEndImageContext();

    lastPoint = currentPoint;
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    CGFloat red,green,blue;

    if(!mouseSwiped) {

      UIGraphicsBeginImageContext(self.mainImage.frame.size);
      ;
      CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
      CGContextSetLineWidth(UIGraphicsGetCurrentContext(), );

      [ getRed:&amp;red green:&amp;green blue:&amp;blue alpha:nil];
      CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red, green, blue, );
      CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y - self.mainImage.frame.origin.y);
      CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y - self.mainImage.frame.origin.y );
      CGContextStrokePath(UIGraphicsGetCurrentContext());
      CGContextFlush(UIGraphicsGetCurrentContext());
      self.tempDrawImage.image = UIGraphicsGetImageFromCurrentImageContext();
      UIGraphicsEndImageContext();
    }

    UIGraphicsBeginImageContext(self.mainImage.frame.size);

    alpha:1.0];
    alpha:];
    if(self.drawMode != DrawEraser)
    {
      self.mainImage.image = UIGraphicsGetImageFromCurrentImageContext();
      self.tempDrawImage.image = nil;
    }
    UIGraphicsEndImageContext();
    mouseSwiped = NO;
}
</code></pre>

<p>这段代码在一个小框架上工作得很好,但是当我将框架增加到比以前大 2 倍时,不幸的是性能不是很好。所以我在考虑优化代码。我特别关注 touchesMoved 方法。据我了解,它在上下文中绘制整个图像并对其进行一些更改并将上下文分配给图像。绘制整个图像似乎重载。所以我想知道,是否可以将图像的某些部分绘制到上下文中并进行一些更改,然后将上下文的这一部分绘制到图像中。 </p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>你是对的 - 每次在 touchesMoved 中重绘整个图像是个坏主意。我认为您应该在开始时创建并保留对上下文的引用。在移动的触摸中,您应该利用它并从上下文中创建图像。您可以使用 <code>CGBitmapContextCreate()</code> 来创建上下文而不是 <code>UIGraphicsBeginImageContext()</code> 和 <code>CGBitmapContextCreateImage()</code> 来从上下文创建图像而不是 <code>UIGraphicsGetImageFromCurrentImageContext()</code>。这是有关如何使用它们的文档( <a href="https://developer.apple.com/library/mac/#documentation/graphicsimaging/reference/CGBitmapContext/Reference/reference.html" rel="noreferrer noopener nofollow">https://developer.apple.com/library/mac/#documentation/graphicsimaging/reference/CGBitmapContext/Reference/reference.html</a> )。</p></p>
                                   
                                                <p style="font-size: 20px;">关于iOS 绘画优化,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/17723696/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/17723696/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: iOS 绘画优化