如何在IOS sdk中屏蔽图像? [英] How to mask an image in IOS sdk?

查看:172
本文介绍了如何在IOS sdk中屏蔽图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在图像上绘制一个单词时应用图像过滤器或蒙版。单词将具有透明效果以透视背景图像。
是否可以在本机IOS sdk或我需要不同的api来执行此操作。
此图片由2张图片组成。一个是印度写的地方,另一个是通过印度信看到的。

I want to apply an image filter or mask when a word drawn on the image.The word will have transparent effect to see through the background image. Is it possible in native IOS sdk or i need different api to perform this. This image consist of 2 images. one is where India is written over, and another one is which is see through India letter.

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

This is the code i am using to generate image from text.

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

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

It is working fine, however i need to generate the image which will have black background and transparent text to see through it.

推荐答案

您可以使用此代码(来自< a href =http://iphonedevelopertips.com/cocoa/how-to-mask-an-image.html =noreferrer>这里),

You can use this code(got from 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];

}

这篇关于如何在IOS sdk中屏蔽图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆