2

我正在使用混合模式目的地通过触摸擦除图像。我成功地做到了这一点。

实际上,我正在通过触摸降低 alpha,因此我也可以设置强度。
现在我的问题是关于用触摸重新绘制图像的擦除部分(即我想用强度绘制图像或者想将 alpha 设置为更暗)。为此,我有原始图像的备份,然后我裁剪了触摸部分并将其与图像合并。但问题是它画的比它应该画的多。

请注意,重绘过程只是在绘制重叠时使图像比原始图像更暗(需要设置上限)。那么如何避免在我已经绘制图像的点处重新绘制以避免原始图像变暗。

我还附上了代码。

// Code to erase an image  


        UIGraphicsBeginImageContext(self._overlayImage.image.size);
        CGRect rect =CGRectMake(0, 0, self._overlayImage.image.size.width, self._overlayImage.image.size.height) ;
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGImageRef imageRef=self._overlayImage.image.CGImage;

        if (imageRef) {
            // Restore the screen that was previously saved
            CGContextTranslateCTM(context, 0, rect.size.height);
            CGContextScaleCTM(context, 1.0, -1.0);

            CGContextDrawImage(context, rect, imageRef);
            //CGImageRelease(imageRef);

            CGContextTranslateCTM(context, 0, rect.size.height);
            CGContextScaleCTM(context, 1.0, -1.0);
        }

        // Erase the background -- raise the alpha to clear more away with eash swipe
        //  [[UIImage imageNamed:@"eraser222.png"] drawAtPoint:point blendMode:kCGBlendModeDestinationOut alpha:.2];
        [ [UIImage imageNamed:@"eraser222.png"] drawInRect:CGRectMake(newPoint.x-self.imgOrignal.size.width*2*radius/self._overlayImage.bounds.size.width, newPoint.y-self.imgOrignal.size.height*2*radius/self._overlayImage.bounds.size.height, self.imgOrignal.size.width*2*radius/self._overlayImage.bounds.size.width, self.imgOrignal.size.height*2*radius/self._overlayImage.bounds.size.height) blendMode:kCGBlendModeDestinationOut alpha:strength/3];

        self._overlayImage.image=UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();


// code to draw an image

 UIImage *cropped = [self imageByCropping:self.imgOrignal toRect:CGRectMake(newPoint.x-self.imgOrignal.size.width*2*radius/self._overlayImage.bounds.size.width, newPoint.y-self.imgOrignal.size.height*2*radius/self._overlayImage.bounds.size.height, self.imgOrignal.size.width*2*radius/self._overlayImage.bounds.size.width, self.imgOrignal.size.height*2*radius/self._overlayImage.bounds.size.height)];
 UIGraphicsBeginImageContext(self._overlayImage.image.size);
        CGRect rect =CGRectMake(0, 0, self._overlayImage.image.size.width, self._overlayImage.image.size.height) ;
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGImageRef imageRef=self._overlayImage.image.CGImage;

        if (imageRef) {
            // Restore the screen that was previously saved
            CGContextTranslateCTM(context, 0, rect.size.height);
            CGContextScaleCTM(context, 1.0, -1.0);

            CGContextDrawImage(context, rect, imageRef);
            //CGImageRelease(imageRef);

            CGContextTranslateCTM(context, 0, rect.size.height);
            CGContextScaleCTM(context, 1.0, -1.0);
        }




        [ cropped drawInRect:CGRectMake(newPoint.x-self.imgOrignal.size.width*2*radius/self._overlayImage.bounds.size.width, newPoint.y-self.imgOrignal.size.height*2*radius/self._overlayImage.bounds.size.height, self.imgOrignal.size.width*2*radius/self._overlayImage.bounds.size.width, self.imgOrignal.size.height*2*radius/self._overlayImage.bounds.size.height) blendMode:kCGBlendModeNormal alpha:strength];



        cropped= [UIImage imageWithData:UIImagePNGRepresentation(cropped)];


        UIImage *finalimage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();


        self._overlayImage.image=finalimage;
4

1 回答 1

1

我知道在这里回答为时已晚,但我已经找到了一种解决方案,我想在这里分享,因为它可以帮助有需要的人。无法删除和重绘相同的图像。所以我处理了两张图片。一个是原始图像,第二个图像是零。第二张图片用于绘制用户触摸屏幕的路径。此后创建一个新的上下文,绘制原始图像,然后使用 kCGBlendModeDestinationOut 混合模式绘制路径图像。它是 kCGBlendModeDestinationOut 这个方法的主角。主要任务是通过使用 kCGBlendModeDestinationOut 混合模式完成的。从而获得所需的效果。在这里查看我的博客。

于 2016-03-15T11:26:08.787 回答