gpt4 book ai didi

iphone - 如何在 IOS sdk 中屏蔽图像?

转载 作者:技术小花猫 更新时间:2023-10-29 11:18:20 25 4
gpt4 key购买 nike

我想在图像上绘制文字时应用图像滤镜或 mask 。该文字将具有透明效果以透过背景图像看到。在 native IOS sdk 中是否有可能,或者我需要不同的 api 来执行此操作。此图像由 2 个图像组成。一种是印度被覆盖的地方,另一种是透视印度的字母。 This image consist of 2 images. one is where India is written over, and another one is which is see through the India letter.

这是我用来从文本生成图像的代码。

-(UIImage *)imageFromText:(NSString *)text{
// set the font type and size
UIFont *font = [UIFont systemFontOfSize:100.0];
CGSize size = [text sizeWithFont:font];

// check if UIGraphicsBeginImageContextWithOptions is available (iOS is 4.0+)
if (UIGraphicsBeginImageContextWithOptions != NULL)
UIGraphicsBeginImageContextWithOptions(size,NO,0.0);
else
// iOS is < 4.0
UIGraphicsBeginImageContext(size);

// optional: add a shadow, to avoid clipping the shadow you should make the context size bigger
//
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSetShadowWithColor(ctx, CGSizeMake(0.0, 1.0), 5.0, [[UIColor blackColor] CGColor]);
CGContextSetBlendMode(ctx,kCGBlendModeNormal);
CGContextSetFillColorWithColor(ctx, [UIColor whiteColor].CGColor);


/*NSLog(@"Rect %@",CGContextGetClipBoundingBox(ctx));
CGImageRef alphaMask = CGBitmapContextCreateImage(ctx);
CGContextClipToMask(ctx, CGContextGetClipBoundingBox(ctx), alphaMask);*/


// draw in context, you can use also drawInRect:withFont:
[text drawAtPoint:CGPointMake(0.0, 0.0) withFont:font];

// transfer image
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

return image;}

它工作正常,但是我需要生成具有黑色背景和透明文本的图像才能看穿它。

最佳答案

您可以使用此代码(来自 here ),

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

}

关于iphone - 如何在 IOS sdk 中屏蔽图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9872975/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com