2011-02-15 8 views
8

Tengo una vista que tiene subcapas cuyos contenidos son imágenes.¿Obtienes UIImage de las capas de vista?

Esperaba obtener la imagen de la vista en myView.image, pero aparentemente la vista no tiene solo imágenes.

¿Cómo creo un UIImage de las capas de la vista?

Respuesta

19
UIGraphicsBeginImageContext(yourView.bounds.size); 
[yourView.layer renderInContext:UIGraphicsGetCurrentContext()]; 
UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

Nota: debe incluir QuartzCore para esto.

1
CGRect myRect =CGRectMake(0, 0, 300, 300);// [self.view bounds]; 
UIGraphicsBeginImageContext(myRect.size); 

CGContextRef ctx = UIGraphicsGetCurrentContext(); 
[[UIColor blackColor] set]; 
CGContextFillRect(ctx, myRect); 

[self.view.layer renderInContext:ctx]; 
UIImage *image1 = UIGraphicsGetImageFromCurrentImageContext(); 
Cuestiones relacionadas