Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How could I get CGContextRef from UIImage?

    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 short, what I want to do is [ UIImage -> make some transform -> transformed UIImage].

Any ideas? Big thanks!!

like image 486
Kordan Ou Avatar asked Dec 14 '10 09:12

Kordan Ou


1 Answers

I think what you need is this:

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

Note that UIGraphicsGetImageFromCurrentImageContext() returns an autoreleased UIImage instance.

like image 154
Costique Avatar answered Oct 18 '22 05:10

Costique