使用touch ios擦除imageView的背景颜色

我想尝试做:我有一个带图像的imageView,我添加了另一个相同尺寸的图像视图与浅文本颜色背景。 我需要清除/擦除上面图像的部分以进行擦除。 因此,从该部分,下面的图像将是清楚的。 我附上的图片 在此处输入图像描述 当我在屏幕上移动触摸时,该部分应该是清晰的。 请帮忙

创建一个Scratch-view类并将您的Scratch transparent图像放在initMethod如: –

 - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { scratchable = [UIImage imageNamed:@"scratchable.jpg"].CGImage; width = CGImageGetWidth(scratchable); height = CGImageGetHeight(scratchable); self.opaque = NO; CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceGray(); CFMutableDataRef pixels = CFDataCreateMutable( NULL , width * height ); alphaPixels = CGBitmapContextCreate( CFDataGetMutableBytePtr( pixels ) , width , height , 8 , width , colorspace , kCGImageAlphaNone ); provider = CGDataProviderCreateWithCFData(pixels); CGContextSetFillColorWithColor(alphaPixels, [UIColor blackColor].CGColor); CGContextFillRect(alphaPixels, frame); CGContextSetStrokeColorWithColor(alphaPixels, [UIColor whiteColor].CGColor); CGContextSetLineWidth(alphaPixels, 20.0); CGContextSetLineCap(alphaPixels, kCGLineCapRound); CGImageRef mask = CGImageMaskCreate(width, height, 8, 8, width, provider, nil, NO); scratched = CGImageCreateWithMask(scratchable, mask); CGImageRelease(mask); CGColorSpaceRelease(colorspace); } return self; } 

并在Scratch之后创建第二个用于显示背景图像的视图类

Bellow这个例子对你来说非常有用:

https://github.com/oyiptong/CGScratch

在此处输入图像描述