2010-12-14 11 views
6
CGContextRef context = ??; // get from UIImage *oldImage 

    CGContextSaveGState(context); 
    CGRect drawRect = CGRectMake(0, 0, 320, 480); 
    CGContextConcatCTM(context, transform); 
    UIImage *newImage = [[UIImage alloc] init]; 
    CGContextDrawImage(context, drawRect, newImage.CGImage); 
    [newImage drawInRect:CGRectMake(0, 0, 320, 480)]; 

    CGContextRestoreGState(context);

In breve, ciò che voglio fare è [UIImage -> make some transform -> transform UIImage].Come posso ottenere CGContextRef da UIImage?

Qualche idea? Grazie tante!!

risposta

8

Penso che quello che vi serve è questo:

UIGraphicsBeginImageContextWithOptions(newImageSize, YES, 0); 
CGContextRef context = UIGraphicsGetCurrentContext(); 
// drawing commands go here 
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

noti che UIGraphicsGetImageFromCurrentImageContext() restituisce un'istanza UIImage autoreleased.

+0

Grazie, ma cosa ho ottenuto un'immagine capovolta anche non ho fatto niente? –

+0

Se si utilizza Core Graphics per il disegno (ad esempio 'CGContextDrawImage()'), ricordare che utilizza un sistema di coordinate capovolte. I metodi di disegno UIImage non richiedono il pre-ribaltamento del sistema di coordinate. – Costique

+0

Domanda Noob: Questo sembra orribilmente sprecone ... Voglio solo aggiungere un po 'di dati ontop della mia immagine. Odio ridisegnare l'intera cosa per questo. – Ralphleon