我想这样做:我有一个带图像的imageView,我添加了另一个相同尺寸的图像视图和浅文本颜色背景。我需要清除/擦除上面图像的部分以进行擦除。因此,从该部分,下面的图像将是清楚的。我附加的图片
当我在屏幕上移动触摸时,该部分应该是清晰的。
请帮忙
答案 0 :(得分:0)
创建一个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