2010-06-19 14 views

risposta

7

Ti piace questa:

UIGraphicsBeginImageContext(myView.bounds.size); 
[myView.layer renderInContext:UIGraphicsGetCurrentContext()]; 
UIImage *myImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

Sarà necessario includere il quadro CoreGraphics, e importare il CALayer.h:

#import <QuartzCore/CALayer.h> 
+0

Esattamente quello che stavo cercando! –

+0

So che questo è vecchio, ma per i display Retina usare 'UIGraphicsBeginImageContextWithOptions (myView.bounds.size, myView.opaque, 0.0);' per evitare un'immagine sfocata. – Jakar

2

Ecco, provate questo

CGImageRef screen = UIGetScreenImage(); 
UIImage *screenImage = [UIImage imageWithCGImage:screen]; 

che avrà uno screenshot dello schermo, quindi in teoria catturare tutti gli elementi della vista e ti dà un UIImage lavorare fuori. Spero possa aiutare!

+0

Ehi, questo è abbastanza bello ma il problema è che cattura l'intera vista principale e ho una barra delle schede, ad esempio nella mia principale w che non voglio nel finale UIImage ... Un modo per aggirare questo? Grazie in anticipo, -David – bobbypage

+0

potresti provare a ritagliare UIImage con le esatte coordinate x, y necessarie. Se questa non è una buona opzione per te, probabilmente questo metodo non funzionerà. –

+0

Forse potresti rendere gli elementi non visibili in tale fase quando l'immagine viene catturata? – KTOV

0

Add seguente metodo per categoria UIView e utilizzare

- (UIImage*) capture { 
    UIGraphicsBeginImageContext(self.bounds.size); 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    [self.layer renderInContext:context]; 
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    return img; 
} 
Problemi correlati