2012-03-26 16 views
9

Quiero aplicar un filtro de imagen o máscara cuando una palabra se dibuja en la imagen. La palabra tendrá un efecto transparente para ver a través de la imagen de fondo. ¿Es posible en SDK nativo de IOS o necesito api diferente para realizar esto? Esta imagen consiste en 2 imágenes. una es donde está escrita la India, y otra es la carta de India. This image consist of 2 images. one is where India is written over, and another one is which is see through the India letter.¿Cómo enmascarar una imagen en IOS SDK?

Este es el código que estoy usando para generar imágenes a partir de texto.

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

Se está trabajando muy bien, sin embargo lo que necesito para generar la imagen que tendrá el fondo negro y texto transparente para ver a través de él.

+0

gracias chicos. esta funcionando. 2 imágenes todo lo que necesitamos es una imagen enmascaradora con fondo negro y una carta incrustada y otra es una imagen original en la que funcionará la máscara. ¿Podría ser posible hacer la imagen de enmascaramiento dinámicamente desde el código? – arindam

Respuesta

13

Usted puede utilizar este código (obtuvo de 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]; 

} 
+0

Esto no funciona para mí en Swift 4 y obtengo la imagen original como la imagen enmascarada. – iAviator

Cuestiones relacionadas