我试着用How to load JPG file into NSBitmapImageRep?回答我的问题,但是我得到了:
“不兼容的指针类型使用类型为'nsimagerep *‘的表达式初始化'nsbitmapimagerep *'”。关于以下内容:
NSImage *controlImage = [[NSImage alloc] initWithContentsOfFile:filePath];
NSBitmapImageRep *imageRep = [[controlImage representations] objectAtIndex:0]; //"incompatible pointer types initializing 'nsbitmapimagerep *' with an expression of type 'nsimagerep *'"发布于 2017-07-11 04:33:00
编译器是正确的。NSImage的表示是NSImageRep的NSArray (参见文档):
@property(readonly, copy) NSArray <NSImageRep *> *representations编译器消息只是一个警告,因此您可以转换第二行并使用直接方法(不使用NSImage)来获得NSBitmapImageRep:
NSBitmapImageRep *imageRep =
(NSBitmapImageRep *)[NSBitmapImageRep imageRepWithContentsOfFile:filePath];另一种避免(有点丑陋的造型)的方法是:
NSData *imgData = [NSData dataWithContentsOfFile:filePath];
NSBitmapImageRep *imageRep = [NSBitmapImageRep imageRepWithData:imgData];警告:此版本在filePath中不计算@2等!
发布于 2017-07-11 04:29:54
NSBitmapImageRep是NSImageRep的一个子类,是“半抽象”的。NSImage还可以存储其他类型的表示,比如用于EPS图像的NSEPSImageRep。
如果你确定它是一个位图,你应该能够简单地转换它。
https://stackoverflow.com/questions/45019922
复制相似问题