2011-12-14 23 views
19
CALayer *sublayer = [CALayer layer]; 
/*sublayer.backgroundColor = [UIColor blueColor].CGColor; 
sublayer.shadowOffset = CGSizeMake(0, 3); 
sublayer.shadowRadius = 5.0; 
sublayer.shadowColor = [UIColor blackColor].CGColor; 
sublayer.shadowOpacity = 0.8;*/ 
sublayer.frame = CGRectMake(30, 100, 256, 256); 
sublayer.contents = (id)[[UIImage imageNamed:@"moon.png"] CGImage]; 
[self.view.layer addSublayer:sublayer]; 
//self.view.backgroundColor = [UIColor blackColor]; 

//add moon mask 
UIGraphicsBeginImageContextWithOptions(CGSizeMake(400, 400), YES, 1); 
CGContextRef contextRef = UIGraphicsGetCurrentContext(); 
CGContextClearRect(contextRef, CGRectMake(0, 0, 400, 400)); 
CGContextSetRGBFillColor(contextRef, 1, 1, 1, 0.8); 
CGContextSetRGBStrokeColor(contextRef, 0, 0, 0, 0.5); 
CGRect ellipse = CGRectMake(50, 50, 128, 128); 
CGContextAddEllipseInRect(contextRef, ellipse); 
CGContextFillEllipseInRect(contextRef, ellipse); 

CALayer* sublayer2 = [CALayer layer]; 
sublayer2.frame = CGRectMake(30, 100, 256, 256); 
sublayer2.backgroundColor = [UIColor clearColor].CGColor; 
sublayer2.contents = (id)[UIGraphicsGetImageFromCurrentImageContext() CGImage]; 
[self.view.layer addSublayer:sublayer2]; 

Este es el código que he escrito hasta ahora. Primero hago capa con imagen de luna. Luego agrego una segunda capa con un dibujo personalizado que debe cubrir parte de la luna. Sin embargo, contextRef hace que su fondo sea negro, así que cuando coloco la segunda capa, primero es invisible. ¿Hay alguna manera de que pueda resolver esto? Mejor aún, ¿existe una mejor manera de hacer un dibujo personalizado programáticamente y agregarlo a la capa?Iphone ¿Cómo hacer que el fondo del contexto sea transparente?

Respuesta

49
UIGraphicsBeginImageContextWithOptions(CGSizeMake(400, 400), NO, 1); 

Establecer este argumento en NO resuelve mi problema.

+0

En caso de que se lo pregunte, ese argumento es el valor 'opaco': https://developer.apple.com/reference/uikit/1623912-uigraphicsbeginimagecontextwitho?language=objc – Senseful

Cuestiones relacionadas