Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to mask an image in IOS sdk?

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 image consist of 2 images. one is where India is written over, and another one is which is see through the 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.

like image 446
arindam Avatar asked Mar 26 '12 13:03

arindam


People also ask

What is image segmentation mask?

A mask is a binary image consisting of zero- and non-zero values. If a mask is applied to another binary or to a grayscale image of the same size, all pixels which are zero in the mask are set to zero in the output image.

What is masking in Swift?

SwiftUI gives us the mask() modifier for masking one with another, which means you can mask an image using text or an image using an image, or more. For example, this creates a 300x300 image of stripes, then masks it using the text “SWIFT!” so that the letters act as a cut out for the image: Image("laser-show") .


1 Answers

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

}
like image 150
Vignesh Avatar answered Oct 10 '22 01:10

Vignesh