2010-08-16 4 views

Répondre

46

#import "QuartzCore/QuartzCore.h" après avoir ajouté le cadre de votre projet. Ensuite, faites:

UIGraphicsBeginImageContext(yourView.frame.size); 
[[yourView layer] renderInContext:UIGraphicsGetCurrentContext()]; 
UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

// The result is *screenshot 
+0

Merci! C'est ce que je cherchais. (Vous avez manqué un ";") –

+0

Merci, j'ai mis à jour mon code :)! – elslooo

2

J'ai utilisé la réponse pour l'améliorer encore davantage. Malheureusement, le code original n'a produit qu'une image en miroir.

Alors, voici le code de travail:

- (UIImage *) imageFromView:(UIView *)view { 

    UIGraphicsBeginImageContext(view.frame.size); 
    CGContextRef currentContext = UIGraphicsGetCurrentContext(); 
    CGContextTranslateCTM(currentContext, 0, view.size.height); 
    // passing negative values to flip the image 
    CGContextScaleCTM(currentContext, 1.0, -1.0); 
    [[appDelegate.scatterPlotView layer] renderInContext:currentContext]; 
    UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    return screenshot; 
}