2010-11-16 7 views
12

Je prends une capture d'écran dans mon application. Je suis capable de prendre la capture d'écran.UIGraphicsBeginImageContext avec les paramètres

Maintenant, je veux prendre la capture d'écran en spécifiant les coordonnées x et y. Est-ce possible?

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

Répondre

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

Merci pour la réponse ... Y at-il un moyen de chager sa hauteur et sa largeur? – user198725878

+0

@barbgal: Utilisez CGContextScaleCTM pour redimensionner. En outre, vous pouvez avoir besoin d'un contexte plus petit (remplacez l'argument par UIGraphicsBeginImageContext). – kennytm

+2

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

2

Si vous utilisez une version plus récente dispositif d'affichage de la rétine, votre code devrait tenir compte de la résolution à l'aide UIGraphicsBeginImageContextWithOptions au lieu 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(); 

Cela rend l'affichage de la rétine contexte d'image .

0

Il suffit de faire quelque chose comme cela, beaucoup plus facile que tous les calculs complexes

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

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

    UIImage *result = UIGraphicsGetImageFromCurrentImageContext(); 

    UIGraphicsEndImageContext(); 

    return result; 
} 
0

Voici la version Swift

//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

la version rapide

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) 
Questions connexes