如何掩盖IOS SDK中的图像?

我想要在图像上绘制一个单词时应用图像filter或蒙版。该单词将透过背景图像看透明效果。 有可能在本地的IOS SDK或我需要不同的API来执行此操作。 这张图片包含2张图片。 一个是印度写的,另一个是印度的信。 这张图片包含2张图片。一个是印度写的,另一个是印度的信。

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

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

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

你可以使用这个代码(从这里得到),

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