使用touch ios擦除imageView背景颜色

时间:2013-08-29 07:48:44

标签: ios image-processing

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

1 个答案:

答案 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

enter image description here

相关问题