iOS 中的 UIImage 或 CGImage API 是否可以实现这种屏蔽

发布于 2024-10-16 02:45:12 字数 513 浏览 2 评论 0原文

我有一个带有一些文本的 UIImage,并且想应用图案 UIImage 作为遮罩。这可能吗? 据我所知,通过 UILabel,我们可以使用 CAGradientLayer 获得这种渐变。但是如果源是 UIImage 可以这样做吗?

图像可能有一些符号/图片等,而不是常规字符,因此是 UIImage。我还可以根据上下文应用不同的遮罩图案来重复使用图像。

这可能吗?

感谢您的帮助。

编辑:感谢您的所有回答。 我了解将渐变应用于文本标签或创建包含文本的图像。 但我的目标是得到这个。--> 单击此处

即我有一个 png,其中有一些绘图,例如具有透明背景的花朵。我想在运行时使用gradient.png 将渐变应用于该图片内的对象,如上图所示。通过掩蔽可以吗?

谢谢

I have an UIImage with some text and would like to apply pattern UIImage as masking. Is this possible ?
I understand that with UILabel we can get this kind of gradient using CAGradientLayer. But can this be done if the source is an UIImage ?

The image may have some symbols/pictures etc other than regular characters and hence UIImage. Also i could reuse the image by applying different masking pattern depending on the context.

is this possible ?

Appreciate your help.

EDIT: Thanks for all your answers.
I understand applying the gradient to a text label or creating an image that has text.
But my goal is to get this.--> Click here

i.e. i have a png with some drawing like a flower with transparent background. I want to apply the gradient to the object inside that picture at runtime with a gradient.png as shown in the picture above. Is that possible with masking ?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

仙气飘飘 2024-10-23 02:45:12

看起来您应该能够使用 CGImageMaskCreate:

- (UIImage*) maskImage:(UIImage *)image withMask:(UIImage *)maskImage {

    CGImageRef maskRef = maskImage.CGImage; 

    CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(maskRef),
        CGImageGetHeight(maskRef),
        CGImageGetBitsPerComponent(maskRef),
        CGImageGetBitsPerPixel(maskRef),
        CGImageGetBytesPerRow(maskRef),
        CGImageGetDataProvider(maskRef), NULL, false);

    CGImageRef masked = CGImageCreateWithMask([image CGImage], mask);
    return [UIImage imageWithCGImage:masked];

}

要进行更长的讨论,请查看此处的评论线程:
http://iosdevelopertips.com/cocoa/how-to-mask- an-image.html

Looks like you should be able to use CGImageMaskCreate:

- (UIImage*) maskImage:(UIImage *)image withMask:(UIImage *)maskImage {

    CGImageRef maskRef = maskImage.CGImage; 

    CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(maskRef),
        CGImageGetHeight(maskRef),
        CGImageGetBitsPerComponent(maskRef),
        CGImageGetBitsPerPixel(maskRef),
        CGImageGetBytesPerRow(maskRef),
        CGImageGetDataProvider(maskRef), NULL, false);

    CGImageRef masked = CGImageCreateWithMask([image CGImage], mask);
    return [UIImage imageWithCGImage:masked];

}

For a longer discussion check out the comment thread here:
http://iosdevelopertips.com/cocoa/how-to-mask-an-image.html

菩提树下叶撕阳。 2024-10-23 02:45:12

是的,确实如此:)

textField.textColor = [UIColor colorWithPatternImage:
[UIImage imageNamed:@"rainbowGradient.png"]];

如果您想要严格控制,Jason Whyne 的想法可能会奏效。但我喜欢这个,因为它短了大约 8 行。

Yes, it is :)

textField.textColor = [UIColor colorWithPatternImage:
[UIImage imageNamed:@"rainbowGradient.png"]];

If you want heavy control, Jason Whyne's idea might work. But I like this one, because it's about 8 lines shorter.

寂寞美少年 2024-10-23 02:45:12

这是绘制遮盖某些内容的文本图像的另一种方法。它基于 kCGBlendModeSourceIn 混合模式:您在清晰的背景上绘制文本,然后在整个地方绘制填充。

NSString *theString = ...;
UIFont *theFont = ...;
CGSize stringSize = [theString sizeWithFont:theFont];

// The background must be clear (fully transparent), hence NO as the 2nd argument
UIGraphicsBeginImageContextWithOptions(stringSize, NO, 0);
[theString drawAtPoint:CGPointZero withFont:theFont];
// This effectively colorizes the image. Use a pattern color...
[patternColor set];
UIRectFillUsingBlendMode(CGRectMake(0, 0, stringSize.width, stringSize.height), kCGBlendModeSourceIn);
// ... or an image:
[patternImage drawInRect:CGRectMake(0, 0, stringSize.width, stringSize.height) blendMode:kCGBlendModeSourceIn alpha:1.0f];

UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

Here's just another way to draw an image of text masking something. It's based on kCGBlendModeSourceIn blending mode: you draw text on a clear background and then draw the fill all over the place.

NSString *theString = ...;
UIFont *theFont = ...;
CGSize stringSize = [theString sizeWithFont:theFont];

// The background must be clear (fully transparent), hence NO as the 2nd argument
UIGraphicsBeginImageContextWithOptions(stringSize, NO, 0);
[theString drawAtPoint:CGPointZero withFont:theFont];
// This effectively colorizes the image. Use a pattern color...
[patternColor set];
UIRectFillUsingBlendMode(CGRectMake(0, 0, stringSize.width, stringSize.height), kCGBlendModeSourceIn);
// ... or an image:
[patternImage drawInRect:CGRectMake(0, 0, stringSize.width, stringSize.height) blendMode:kCGBlendModeSourceIn alpha:1.0f];

UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
微凉徒眸意 2024-10-23 02:45:12

使用面罩的样品很好并且效果很好,但是你正在泄漏。
CGImageMaskCreate 和 CGI​​mageCreateWithMask 进行分配(遵循“创建”-> 保留规则)
所以你应该释放面具&使用后的图像:

CGImageRef masked = CGImageCreateWithMask([image CGImage], mask);
UIImage * result =  [UIImage imageWithCGImage:masked];
CGImageRelease(mask);
CGImageRelease(masked);
return result;

根据 ADC 文档:
...
返回值
Quartz 位图图像蒙版。您负责通过调用 CGImageRelease 来释放该对象。

sample using mask is fine and works well, but You are leaking.
CGImageMaskCreate and CGImageCreateWithMask do allocate (following "create" -> retain rule)
so You should release mask & image after using:

CGImageRef masked = CGImageCreateWithMask([image CGImage], mask);
UIImage * result =  [UIImage imageWithCGImage:masked];
CGImageRelease(mask);
CGImageRelease(masked);
return result;

As per ADC docs:
...
Return Value
A Quartz bitmap image mask. You are responsible for releasing this object by calling CGImageRelease.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文