为什么响应速度慢,因为使用UItouch的笔画PNG图像?

时间:2013-11-03 16:36:11

标签: ios objective-c uiimage cgcontext uitouch

我用下面的代码用手指移动来抚摸PNG。有2个UIImage视图。一个位于背景,将背景图像放在那里。另一个是清晰的UIImage视图,用于在其上方描绘PNG图像。

  -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
   {
       for (UITouch * touch in touches) {

          currentPoint = [touch locationInView:self.view];
          lastPoint = [touch previousLocationInView:self.view];

    //set up array to make space between PNG images
          if (ABS(currentPoint.x-lastPoint.x)>16
               || ABS(currentPoint.y - lastPoint.y) > 13) {

              [brushLocations addObject:[NSValue valueWithCGPoint:currentPoint]];


      }
        [self drawingWithArray];

 }

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

         [brushLocations removeAllObjects];//reset


     }


 -(void)drawingWithArray{



     UIGraphicsBeginImageContext(self.view.frame.size);
     [drawImage.image drawInRect:CGRectMake(0, 0, drawImage.frame.size.width,    drawImage.frame.size.height)];

     for (int i=0; i<[brushLocations count]; i++) {

 CGPoint center =[[brushLocations objectAtIndex:i]CGPointValue];


    // bokehImage is UIImage 

         bokehImage=[bokehImgArray objectAtIndex: i%[bokehImgArray count]];

 /// the PNG images are not semi-transparent, even set the alpha is 0.5??

         [bokehImage drawAtPoint:center blendMode:kCGBlendModeOverlay alpha:0.5f];

//drawImage is uiimage view on top of background image view for stroke PNG images.
     drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
     UIGraphicsEndImageContext();

}

现在,我遇到的问题是响应缓慢。当手指在设备上移动(IPad4)时,PNG图像不会立即显示。

此外,PNG图像不是半透明的。我认为“drawAtPoint .. blendMode .. alpha”的功能可以使图像半透明(设置为0.5 alpha)。

1 个答案:

答案 0 :(得分:0)

是的,这样的事情应该有效:

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    for (UITouch * touch in touches) {
        currentPoint = [touch locationInView:self.view];
        lastPoint = [touch previousLocationInView:self.view];
        //set up array to make space between PNG images
        if (ABS(currentPoint.x-lastPoint.x)>16
            || ABS(currentPoint.y - lastPoint.y) > 13) {
            [brushLocations addObject:[NSValue valueWithCGPoint:currentPoint]];
        }
//        [self drawingWithArray]; // don't call draw routine during touch handler
         [self setNeedsDisplay]; // queue the redraw instead
    }
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
// Not needed here
//    [brushLocations removeAllObjects];//reset
}

//-(void)drawingWithArray
- (void)drawRect:(CGRect)rect
{
    // CGContext is already set when drawRect is called
//    UIGraphicsBeginImageContext(self.view.frame.size);
//    [drawImage.image drawInRect:CGRectMake(0, 0, drawImage.frame.size.width, drawImage.frame.size.height)];
    [drawImage.image drawInRect:rect];
    for (int i=0; i<[brushLocations count]; i++) {
        CGPoint center =[[brushLocations objectAtIndex:i]CGPointValue];
        // bokehImage is UIImage
        bokehImage=[bokehImgArray objectAtIndex: i%[bokehImgArray count]];
        // the PNG images are not semi-transparent, even set the alpha is 0.5??
        [bokehImage drawAtPoint:center blendMode:kCGBlendModeOverlay alpha:0.5f];
        //drawImage is uiimage view on top of background image view for stroke PNG images.
        drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
//        UIGraphicsEndImageContext();
    }
    [brushLocations removeAllObjects];//reset
}