15
A
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
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;
}
Merci! C'est ce que je cherchais. (Vous avez manqué un ";") –
Merci, j'ai mis à jour mon code :)! – elslooo