我有它,用户可以在屏幕上移动图像和 uilabel。假设它是黑色图像和黑色文本。我希望与图像重叠的文本部分变得透明,以便背景显示出来,即使它们最初是相同的颜色,您仍然可以阅读文本。希望这是有道理的。
我找到了一些关于触摸擦除的链接。我不确定如何使用重叠的文本部分自动执行此操作。
也许像这样“用图像掩盖图像”? https://developer.apple.com/library/mac/#documentation/graphicsimaging/conceptual/drawingwithquartz2d/dq_images/dq_images.html
这是我移动图像(以及文本)的方式
-(void)graphicMoved:(UIPanGestureRecognizer *)recognizer
{
CGPoint translation = [recognizer translationInView:self.view];
recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x,
recognizer.view.center.y + translation.y);
[recognizer setTranslation:CGPointMake(0, 0) inView:self.view];
}
添加的代码 在将文本和图像部分重叠放置后,我在单击按钮时调用 creatAlphaClippedImage。图像是 imageMerge,标签是 labelTest。
我所做的只是在开始时分配它们,然后在最后,获取输出图像并将其分配给 imageMerge.image,该图像被添加到我的视图中,界面生成器在角落里只是为了测试。我不知道 nslog 应该显示什么,但它不是空的。
-(void)createAlphaClippedImage {
UIImageView *imageView = nil;//this will b your imageView, property that u are holding to
UILabel *label = nil;//this will be your label, property that u are holding to
imageView=imageMerge;
label=labelTest;
if (CGRectIntersectsRect(imageView.frame, label.frame)) {
UIImage *bottomImage = imageView.image;
UIImage *image = nil;
UIGraphicsBeginImageContextWithOptions(bottomImage.size, NO, 0.0);
CGRect imageRect = CGRectMake(0.0f, 0.0f, bottomImage.size.width, bottomImage.size.height);
[bottomImage drawInRect:imageRect];
//the color that u want the text to have
[[UIColor clearColor] set];
//change this textRect to change the position of text on image
CGRect textRect = CGRectMake(CGRectGetMinX(label.frame) - CGRectGetMinX(imageView.frame), CGRectGetMinY(label.frame) - CGRectGetMinY(imageView.frame), CGRectGetWidth(label.frame), CGRectGetHeight(label.frame));
[label.text drawInRect:textRect withFont:label.font];
image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
imageView.image = image;
imageMerge.image=image;
NSLog(@"ImageMerge %@\n",imageMerge.image);
}
}