与文字重叠从一个UILabel的图像的擦除部分(Erasing part of an image t

2019-08-16 21:39发布

我有它,用户可以在移动的图像,并在屏幕上的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。

我要做的就是在一开始为它们分配,然后在年底,以输出impage并将其分配给其添加到我的观点与界面生成器在角落里只是为了测试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);
}
}

Answer 1:

假设你已经通过与在屏幕上移动你的ImageView。 让我做什么进行。

  1. 当你触摸ImageView的。 使用我要提供的代码改变它的形象。
  2. 一旦你完成移动ImageView的,与旧图片更换新形象

新图像=一个用黑色图像+黑色文本; 老图像=一个用黑色图像+透明文本;

-(UIImage *)customImage:(NSString *)cellImageName withText:(NSString *)badgeText textColor:(UIColor*)color {
    UIImage *bottomImage = [UIImage imageNamed:cellImageName];
    //Change the font size to change text size
    CGSize stringSize = [badgeText sizeWithFont:[UIFont systemFontOfSize:[UIFont systemFontSize]]];

    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
    [color set];
    //change this textRect to change the position of text on image
    CGRect textRect = CGRectMake((bottomImage.size.width - stringSize.width)/2.0f, bottomImage.size.height - stringSize.height, stringSize.width, stringSize.height);
    [badgeText drawInRect:textRect withFont:[UIFont systemFontOfSize:[UIFont systemFontSize]]];
    image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return image;
}

现在你开始之前,扔掉的标签和为方便起见(你可以替换的CALayer的UIImageView的,如果你愿意的话),即代替

imageView.image =图片你可以写layer.contents =(ID)image.CGImage

现在,当U设置UR的ImageView或层,然后先获取图像

的UIImage *图像= [自customImage:@ “图像名” withText:@ “文本” 文字颜色:的UIColor blackColor]];

并把这个图像到ImageView的或层

现在,当你开始移动你的ImageView或层。 在touchesBegan或任何方法是触摸你的第一个响应者。 再次更换形象

的UIImage *图像= [自customImage:@ “图像名” withText:@ “文本” 文字颜色:的UIColor clearColor]];

而在touchesEnded与黑色文本的第一个电话再次更换图像。

这应该让你去。 让我知道在出现问题时。

上述方法提供了写在图片上方的文字,并创建它的文本的新形象。 因此,你可以扔掉的标签。

欢呼声中,有乐趣。

编辑

试试这个,还没有尝试过自己,但应该工作

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

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

调用的touchesBegan和touchesMoved此方法。 如果动画口吃(这shud没有发生),尝试调用它的动画块内。

试试这个,让我知道。

编辑:链接到示例应用程序

我创建展示如何清除重叠区域的样本应用程序。 核实。 https://www.dropbox.com/sh/5z45gkwgmstfyvu/lwzbUyh6IR



文章来源: Erasing part of an image that overlaps with text from a UILabel