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

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];

但这看起来极其低效。怎么做呢?
我肯定自己很困惑或者忽略了一些简单的东西,所以谢谢。
1个回答

5

使用CGContextDrawImage

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

壮观 - 就是这样。你是最棒的。非常感谢。 (我刚意识到它是从左到右反过来的,当然!) - Fattie
你仍然是最棒的,而且依然是非常出色的! - Fattie

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接