2012-03-26 15 views
9

Voglio applicare un filtro immagine o maschera quando una parola disegnata sull'immagine. La parola avrà effetto trasparente per vedere attraverso l'immagine di sfondo. E 'possibile in SDK nativo IOS o ho bisogno di API diverse per eseguire questo. Questa immagine è composta da 2 immagini. uno è dove l'India è scritta, e un altro è che è vedere attraverso la lettera in 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.Come mascherare un'immagine in sdk IOS?

Questo è il codice che sto usando per generare l'immagine dal testo.

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

Si sta lavorando bene, però ho bisogno per generare l'immagine che avrà sfondo nero e testo trasparente per vedere attraverso di essa.

+0

grazie ragazzi. sta funzionando. 2 immagini tutto ciò di cui abbiamo bisogno è mascherare l'immagine con sfondo nero e lettera incorporata e un'altra immagine originale su cui lavorerà il mascheramento. Potrebbe essere possibile rendere l'immagine di mascheramento dinamicamente dal codice? – arindam

risposta

13

È possibile utilizzare questo codice (ottenuto da 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

Questo non funziona per me su Swift 4 e ottengo l'immagine originale come immagine mascherata. – iAviator