1

我使用下面的代码通过手指移动来描边 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)。

4

1 回答 1

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
}
于 2013-11-04T17:12:33.947 回答