2010-08-16 17 views

risposta

46

#import "QuartzCore/QuartzCore.h" dopo aver aggiunto il quadro per il vostro progetto. Quindi:

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

// The result is *screenshot 
+0

Grazie! Questo è quello che stavo cercando. (Ti sei perso un ";") –

+0

Grazie, ho aggiornato il mio codice :)! – elslooo

2

Ho usato la risposta per migliorarlo ulteriormente. Sfortunatamente, il codice originale ha prodotto solo un'immagine speculare.

Quindi, ecco il codice di lavoro:

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