2010-11-16 8 views
12

Ich mache einen Screenshot in meiner Anwendung. Ich kann den Screenshot machen.UIGraphicsBeginImageContext mit Parametern

Jetzt möchte ich den Screenshot durch Angabe der X- und Y-Koordinate machen. Ist das möglich?

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

Antwort

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

danke für die Antwort ... Gibt es eine Möglichkeit, seine Höhe und Breite zu ändern. – user198725878

+0

@barbgal: Verwenden Sie CGContextScaleCTM, um die Größe zu ändern. Möglicherweise benötigen Sie auch einen kleineren Kontext (ändern Sie das Argument in UIGraphicsBeginImageContext). – kennytm

+2

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

2

Wenn Sie ein neueres Retina-Display-Gerät verwenden, sollte Sie den Code in der Auflösung Faktor von UIGraphicsBeginImageContextWithOptions statt UIGraphicsBeginImageContext mit:

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(); 

Dies wird die Retina-Display Bild Kontext machen .

0

tun nur so etwas wie dies, so leichter als all die komplexen Berechnungen

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

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

    UIImage *result = UIGraphicsGetImageFromCurrentImageContext(); 

    UIGraphicsEndImageContext(); 

    return result; 
} 
0

Hier Swift Version

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

swift Version

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)