我正在使用混合模式目的地通过触摸擦除图像。我成功地做到了这一点。
实际上,我正在通过触摸降低 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;