我如何从UIImage获得CGContextRef?

Kor*_* Ou 6 iphone uiimage cgcontext

    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);
Run Code Online (Sandbox Code Playgroud)

简而言之,我想要做的是[UIImage - >进行一些转换 - >转换UIImage].

有任何想法吗?十分感谢!!

Cos*_*que 8

我想你需要的是:

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

请注意,UIGraphicsGetImageFromCurrentImageContext()返回一个自动释放的UIImage实例.

  • @Ralphleon"在顶部添加图形"相当于绘图和混合,这需要图形引擎的全部功能. (2认同)