【问题标题】:Erasing part of an image that overlaps with text from a UILabel擦除与 UILabel 中的文本重叠的图像的一部分
【发布时间】:2013-02-14 17:26:41
【问题描述】:

我有它,用户可以在屏幕上移动图像和 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);
}
}

【问题讨论】:

    标签: ios xcode uiimageview masking


    【解决方案1】:

    假设您已经完成了在屏幕上移动 imageView 的操作。让我继续做什么。

    1. 当您触摸 imageView 时。使用我将提供的代码更改其中的图像。
    2. 完成图像视图的移动后,将新图像替换为旧图像

    新图像 = 黑色图像 + 黑色文本;旧图像 = 一张 黑图+透明文字;

    -(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 = image 你可以写 layer.contents = (id)image.CGImage

    现在,当你设置你的 ImageView 或 Layer 时,首先获取图像为

    UIImage *image = [self customImage:@"Image Name" withText:@"Text" textColor:[UIColor blackColor]];

    并将这张图片放入 imageView 或图层中

    现在,当您开始移动图像视图或图层时。在touchesBegan 或任何一种方法是您的第一响应者触摸。再次替换图片为

    UIImage *image = [self customImage:@"Image Name" withText:@"Text" textColor:[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 中调用此方法。如果动画卡顿(这不会发生),请尝试在动画块中调用它。

    试试这个,然后告诉我。

    编辑:链接到示例应用

    我创建了一个示例应用程序,展示如何清除重叠区域。 核实。 https://www.dropbox.com/sh/5z45gkwgmstfyvu/lwzbUyh6IR

    【讨论】:

    • 不是文本的一部分..图像顶部的 UILabel 是您在移动图像时想要使其透明(文本)的部分..或者它是预加载的一些图像 txt ?
    • 如果是前者..在移动图像视图时用新图像替换旧图像,会产生文本已从图像中剪切出来的错觉。因为在上下文中书写时的新颜色是清晰的颜色。
    • 给我你的邮件ID..我会给你一个示例应用程序,展示如何清除重叠区域,从那里你可以通过实现你的触摸移动和触摸取消中的方法来继续你的要求.
    • 在渲染到上下文renderInContext 之前,要获得topImagebottomImage 的层添加这两行CGContextTranslateCTM(context, frameToCut.size.width, frameToCut.size.height); CGContextRotateCTM(context, - M_PI);,而不是- M_PI 提供您的旋转角度。在做所有这些之前,请记住旋转你的视图。
    • 看..无论你旋转..你必须翻译然后旋转图像相同..这就是我给你的两条线。正确使用它,一切都会得到解决..我已经尝试过了..通过将示例标签旋转为 M_PI_2 和 M_PI 两者。正确使用它们,你会得到它:)
    猜你喜欢
    • 2016-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-28
    相关资源
    最近更新 更多