如何重新绘制在iphone中移动的触摸图像

时间:2012-10-18 15:19:04

标签: iphone ios uiimage

我正在使用混合模式目标输出来触摸图像。我成功地做到了这一点。

实际上我通过触摸减少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;

1 个答案:

答案 0 :(得分:1)

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