2010-11-16 10 views
12

Estoy tomando una captura de pantalla en mi aplicación. Puedo tomar la captura de pantalla.UIGraphicsBeginImageContext con los parámetros

Ahora quiero tomar la captura de pantalla especificando la coordenada xey. ¿Es eso posible?

UIGraphicsBeginImageContext(self.view.bounds.size); 
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; 
UIImage* aImage = UIGraphicsGetImageFromCurrentImageContext(); 

Respuesta

18
UIGraphicsBeginImageContext(self.view.bounds.size); 
CGContextRef c = UIGraphicsGetCurrentContext(); 
CGContextTranslateCTM(c, 0, -40); // <-- shift everything up by 40px when drawing. 
[self.view.layer renderInContext:c]; 
UIImage* viewImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 
+0

gracias por la respuesta ... ¿Hay alguna manera de cambiar su altura y ancho? – user198725878

+0

@barbgal: use CGContextScaleCTM para cambiar el tamaño. Además, es posible que necesite un contexto más pequeño (cambie el argumento a UIGraphicsBeginImageContext). – kennytm

+2

@Barbgal: No. 'CGSize size = self.view.bounds.size; UIGraphicsBeginImageContext (CGSizeMake (size.width, size.height-40)); CGContextRef c = UIGraphicsGetCurrentContext(); ' – kennytm

2

Si está utilizando un dispositivo de visualización de la retina más nuevo, el código debe tener en cuenta la resolución mediante el uso de UIGraphicsBeginImageContextWithOptions en lugar de UIGraphicsBeginImageContext:

UIGraphicsBeginImageContextWithOptions(self.view.bounds.size,YES,2.0); 
CGContextRef c = UIGraphicsGetCurrentContext(); 
CGContextTranslateCTM(c, 0, -40); // <-- shift everything up by 40px when drawing. 
[self.view.layer renderInContext:c]; 
UIImage* viewImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

Esto hará que el contexto imagen de la pantalla retina .

0

Sólo hacer algo como esto, manera más fácil que todos esos cálculos complejos

+ (UIImage *)imageWithView:(UIView *)view { 
    UIGraphicsBeginImageContextWithOptions([view bounds].size, NO, [[UIScreen mainScreen] scale]); 

    [[view layer] renderInContext:UIGraphicsGetCurrentContext()]; 

    UIImage *result = UIGraphicsGetImageFromCurrentImageContext(); 

    UIGraphicsEndImageContext(); 

    return result; 
} 
0

Aquí está la versión Swift versión

//Capture Screen 
func capture()->UIImage { 

    UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, false, UIScreen.mainScreen().scale) 
    self.view.layer.renderInContext(UIGraphicsGetCurrentContext()!) 
    let image = UIGraphicsGetImageFromCurrentImageContext() 
    UIGraphicsEndImageContext() 
    return image 

} 
0

rápida

UIGraphicsBeginImageContext(self.view.bounds.size) 
    let image: CGContextRef = UIGraphicsGetCurrentContext()! 
    CGContextTranslateCTM(image, 0, -40) 
    // <-- shift everything up by 40px when drawing. 
    self.view.layer.renderInContext(image) 
    let viewImage: UIImage = UIGraphicsGetImageFromCurrentImageContext() 
    UIGraphicsEndImageContext() 
    UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil)