简体   繁体   English

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

[英]draw CGImageRef (not UIImage) into image context

Here's exactly how to draw a UIImage in to context: 以下是如何在上下文中绘制UIImage:

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

No problem. 没问题。 However, say I have a CGImageRef which was built up somehow. 但是,假设我有一个以某种方式构建的CGImageRef。

CGImageRef happyImageRef = CGImageCreateWithImageInRect(blah blah);

I want to draw it in to the context. 我想把它绘制到上下文中。

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

How do you do that? 你是怎样做的?

To be clear, obviously you can just convert it to a UIImage 要清楚,显然你可以将它转换为UIImage

UIImage *wasteful = [UIImage imageWithCGImage:happyImageRef];

But that seems incredibly inefficient. 但这看起来非常低效。 How to do it? 怎么做?

I'm sure I'm confused or missing something simple, so thanks. 我确定我很困惑或遗漏了一些简单的东西,谢谢。

Use CGContextDrawImage 使用CGContextDrawImage

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM