erase color of imageView's background color wi

2020-07-30 00:15发布

问题:

I am trying to do like : I have one imageView with image and I add another Image view of same dimensions with light Text color background. And i need to clear / erase the portion of above image to erase. So that from that portion, below image will be clear. Image i attached And as i moved touch on screen, that portion should be clear. Any help please

回答1:

Create one Scratch-view class and put your Scratch transparent image in initMethod like:-

- (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;
}

and also create second view class for display image of background after Scratch

Bellow this example is much useful to you try with this:-

https://github.com/oyiptong/CGScratch