如何将JPEG加载到位图图像阵列?

时间:2017-07-10 19:11:29

标签: objective-c macos bitmap jpeg

我尝试使用How to load JPG file into NSBitmapImageRep?来回答我的问题,但我得到了:

"不兼容的指针类型初始化&#ns; nsbitmapimagerep *'表达式为&n ;; nsimagerep *'"。关于:

NSImage *controlImage = [[NSImage alloc] initWithContentsOfFile:filePath];
NSBitmapImageRep *imageRep = [[controlImage representations] objectAtIndex:0]; //"incompatible pointer types initializing 'nsbitmapimagerep *' with an expression of type 'nsimagerep *'"

2 个答案:

答案 0 :(得分:1)

编译器是对的。 NSImage的代表是NSArray的{​​{1}}(参见文档):

NSImageRep

编译器消息只是一个警告,所以 您可以强制转换第二行并使用直接方式(不使用@property(readonly, copy) NSArray <NSImageRep *> *representations )来获取NSBitmapImageRep:

NSImage

避免(有点丑陋的演员)的另一种方法是:

NSBitmapImageRep *imageRep =
     (NSBitmapImageRep *)[NSBitmapImageRep imageRepWithContentsOfFile:filePath];

警告:此版本不评估filePath中的@ 2等等。

答案 1 :(得分:0)

NSBitmapImageRep是NSImageRep的子类,它是&#34; semiabstract&#34;。 NSImage也可以存储其他类型的表示 - 比如NSEPSImageRep,用于EPS图像。

如果你确定它是位图,你应该可以简单地投射它。

相关问题