将CGImageRef(不是UIImage)绘制到图像上下文中

时间:2014-03-06 11:50:51

标签: cgimageref uigraphicscontext drawinrect

以下是如何在上下文中绘制UIImage:

UIGraphicsBeginImageContextWithOptions(size, NO, 0.0f);
[someUIImage drawInRect:CGRectMake(...)];
[someOtherUIImage drawInRect:CGRectMake(...)];
[someOtherUIImage drawInRect:CGRectMake(...)];
UIImage *yourResult = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

没问题。但是,假设我有一个以某种方式构建的CGImageRef。

CGImageRef happyImageRef = CGImageCreateWithImageInRect(blah blah);

我想将它绘制到上下文中。

UIGraphicsBeginImageContextWithOptions(size, NO, 0.0f);
[ happyImageRef .. somehowDrawInRect:CGRectMake(...)];
UIImage *yourResult = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

你是怎么做到的?

要清楚,显然你可以将其转换为UIImage

UIImage *wasteful = [UIImage imageWithCGImage:happyImageRef];

但这似乎非常低效。怎么做?

我确信我很困惑或遗漏了一些简单的东西,谢谢。

1 个答案:

答案 0 :(得分:5)

使用CGContextDrawImage

UIGraphicsBeginImageContextWithOptions(size, NO, 0.0f);
CGContextDrawImage(UIGraphicsGetCurrentContext(),CGRectMake(...), happyImageRef);
UIImage *yourResult = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();