3

下面是在上下文中绘制 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];

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

我确定我很困惑或缺少一些简单的东西,所以谢谢。

4

1 回答 1

5

使用CGContextDrawImage

UIGraphicsBeginImageContextWithOptions(size, NO, 0.0f);
CGContextDrawImage(UIGraphicsGetCurrentContext(),CGRectMake(...), happyImageRef);
UIImage *yourResult = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
于 2014-03-06T12:03:50.577 回答