简体   繁体   English

如何从文件中获取CGImageRef?

[英]How can I get a CGImageRef from a file?

So I know you can get a CGImage from a file using UIImage... 所以我知道您可以使用UIImage从文件获取CGImage ...

UIImage *img = [UIImage imageNamed:@"name.bmp"];
[img CGImage];

But, is there a way to get a CGImageRef without using a UIImage ? 但是,有没有一种方法可以在不使用UIImage情况下获取CGImageRef

(I am trying this in a static library which doesn't have access to UIKit) I could use other frameworks if necessary, just not UIKit. (我正在无法访问UIKit的静态库中尝试此操作)如有必要,我可以使用其他框架,而不能使用UIKit。 Would CIImage work? CIImage工作吗? Or NSBitmapImageRep ? 还是NSBitmapImageRep

You can get it from NSData: 您可以从NSData获得它:

CGDataProviderRef imgDataProvider = CGDataProviderCreateWithCFData((CFDataRef)[NSData dataWithContentsOfFile:@""]);
CGImageRef image = CGImageCreateWithPNGDataProvider(imgDataProvider, NULL, true, kCGRenderingIntentDefault); // Or JPEGDataProvider

You would have to be certain that the data was a JPEG or a PNG for this to work. 您必须确保数据是JPEG或PNG才能起作用。

You have to understand that using [UIImage imageNamed:@"name.bmp"] works through UIKit framework. 您必须了解,使用[UIImage imageNamed:@“ name.bmp”]可以通过UIKit框架使用。 This is not thread safe. 这不是线程安全的。 Example for dispatch_apply you must use Quartz framework. 对于dispatch_apply的示例,必须使用Quartz框架。

// Through Quartz
NSString *maskFilePath = [[NSBundle mainBundle] pathForResource:@"mask" ofType:@"png"];
CGDataProviderRef dataProvider = CGDataProviderCreateWithFilename([maskFilePath UTF8String]);
CGImageRef maskRef = CGImageCreateWithPNGDataProvider(dataProvider, NULL, true, kCGRenderingIntentDefault);

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

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