简体   繁体   English

如何从磁盘上的图像制作CGImageRef

[英]How exactly to make a CGImageRef from an image on disk

I've looked around everywhere to no avail. 我到处寻找无济于事。 I'm doing some image loading in a thread and since UIKit is not thread safe I'm going to have to store the images as CGImageRefs but I can't figure out how to do this. 我正在线程中进行一些图像加载,因为UIKit不是线程安全的,我将不得不将图像存储为CGImageRefs,但我无法弄清楚如何做到这一点。 I haven't played with any of the Quartz stuff before so it's confusing me. 我之前没有使用任何Quartz的东西,所以这让我很困惑。 Basically I just need to load a JPG from the disk into a CGImageRef. 基本上我只需要将磁盘中的JPG加载到CGImageRef中。 Also for bonus points, is there anyway to load a GIF into a CGImageRef? 同样对于奖励积分,无论如何将GIF加载到CGImageRef中?

gcamp is more or less correct, you are pretty safe to use this on a background thread, but to answer your question, I believe the following would work. gcamp或多或少是正确的,你在后台线程上使用它是非常安全的,但是为了回答你的问题,我相信以下内容是可行的。

CGDataProviderRef dataProvider = CGDataProviderCreateWithFilename("file");
CGImageRef image = CGImageCreateWithPNGDataProvider(dataProvider, NULL, NO, kCGRenderingIntentDefault);

There is also a function for jpg images, CGImageCreateWithJPEGDataProvider . 还有jpg图像的功能, CGImageCreateWithJPEGDataProvider

(be sure to release the data provider and image when done) (完成后务必释放数据提供者和图像)

I think the simplest way is to use UIKit and use the CGImage propriety of UIImage . 我认为最简单的方法是使用UIKit并使用UIImageCGImage特性。

Something like that : 像这样的东西:

UIImage* image = [UIImage imageNamed:@"Your Image.png"];
CGImageRef imageRef = image.CGImage;

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

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