问题描述
我想这样做:我有一个带图像的imageView,我添加了另一个相同尺寸的图像视图和浅文本颜色背景。我需要清除/擦除上面图像的部分以进行擦除。因此,从该部分,下面的图像将是清楚的。我附加的图片
当我在屏幕上移动触摸时,该部分应该是清晰的。
任何帮助请
创建一个Scratch-view类并将你的Scratch 透明
图像在 initMethod
中,如: -
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if(self){
scratchable = [UIImage imageNamed:@scratchable.jpg]。CGImage;
width = CGImageGetWidth(可刮擦);
height = CGImageGetHeight(可刮擦);
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(可刮,面具);
CGImageRelease(mask);
CGColorSpaceRelease(colorspace);
}
返回自我;
}
并在Scratch
这个例子对你来说非常有用: -
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
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
这篇关于使用touch ios擦除imageView的背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!