Applicare una maschera ad un immagine

April 15, 2011

Una delle modifiche che è facilmente implementabile ad un immagine tramite applicazione è l'applicazione di una maschera di copertura. In questo breve post vi spiego come fare.

Basta creare la funzione nel file punto m

1
2
3
4
5
6
7
8
9
10
11
- (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];
}

dichiararlo del file punto h

1
- (UIImage*) maskImage:(UIImage *)image withMask:(UIImage *)maskImage;

Attenzione è una funzione e non una procedura!!!!
Questo comporta che bisogna salvare l’immagine risultato! le immagini vanno dichiarate così:

1
2
3
4
UIImage *maschera = [UIImage imageNamed:@"maschera.jpg"];
UIImage *foto = [UIImage imageNamed:@"foto.jpg"];
UIImage *risultato=[[UIImage alloc]init];
risultato = [self maskImage:(UIImage *)foto withMask:(UIImage *)maschera];
Fonte:
http://iphonedevelopertips.com

Privacy Policy