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

时间:2021-06-19 21:24:37

I tried using How to load JPG file into NSBitmapImageRep? to answer my question but I get:

我尝试使用如何将JPG文件加载到NSBitmapImageRep中?回答我的问题,但我得到:

"incompatible pointer types initializing 'nsbitmapimagerep *' with an expression of type 'nsimagerep *'". with respect to:

“不兼容的指针类型初始化'nsbitmapimagerep *',表达式为'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 个解决方案

#1


1  

The compiler is right. The representions of an NSImage are an NSArray of NSImageRep (see the docs):

编译器是对的。 NSImage的代表是NSImageRep的NSArray(参见文档):

@property(readonly, copy) NSArray <NSImageRep *> *representations

The compiler message is only a warning, so you could cast the second line and use the direct way (without using an NSImage) to get an NSBitmapImageRep:

编译器消息只是一个警告,因此您可以强制转换第二行并使用直接方式(不使用NSImage)来获取NSBitmapImageRep:

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

Another way avoiding the (a bit ugly casting) is:

避免(有点难看的铸造)的另一种方法是:

NSData *imgData = [NSData dataWithContentsOfFile:filePath];
NSBitmapImageRep *imageRep = [NSBitmapImageRep imageRepWithData:imgData];

Warning: this version does NOT evaluate an @2 etc. in the filePath!

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

#2


0  

NSBitmapImageRep is a subclass of NSImageRep, which is "semiabstract". NSImage can store other kinds of representations, too -- like NSEPSImageRep, for EPS images.

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

If you're sure it's a bitmap, you should be able to simply cast it.

如果您确定它是位图,您应该能够简单地投射它。

#1


1  

The compiler is right. The representions of an NSImage are an NSArray of NSImageRep (see the docs):

编译器是对的。 NSImage的代表是NSImageRep的NSArray(参见文档):

@property(readonly, copy) NSArray <NSImageRep *> *representations

The compiler message is only a warning, so you could cast the second line and use the direct way (without using an NSImage) to get an NSBitmapImageRep:

编译器消息只是一个警告,因此您可以强制转换第二行并使用直接方式(不使用NSImage)来获取NSBitmapImageRep:

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

Another way avoiding the (a bit ugly casting) is:

避免(有点难看的铸造)的另一种方法是:

NSData *imgData = [NSData dataWithContentsOfFile:filePath];
NSBitmapImageRep *imageRep = [NSBitmapImageRep imageRepWithData:imgData];

Warning: this version does NOT evaluate an @2 etc. in the filePath!

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

#2


0  

NSBitmapImageRep is a subclass of NSImageRep, which is "semiabstract". NSImage can store other kinds of representations, too -- like NSEPSImageRep, for EPS images.

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

If you're sure it's a bitmap, you should be able to simply cast it.

如果您确定它是位图,您应该能够简单地投射它。