NSBitmapImageRep load image

316 views Asked by At

i'm quite new to objective C and followed some instructions i found, but at the moment i got stuck.

i am trying to load an image with the following code:

NSBitmapImageRep *img;
img = [NSBitmapImageRep imageRepWithContentsOfFile:filename];

"filename" is a valid pointer. But the compiler tells me:

Incompatible pointer types assigning to 'NSBitmapImageRep *' from 'NSImageRep * _Nullable' 

What am i missing or doing wrong ?

1

There are 1 answers

1
Borys Verebskyi On BEST ANSWER

imageRepWithContentsOfFile: is a factory method. According documentation, it returns "An initialized instance of an NSImageRep subclass, or nil if the image data could not be read." From NSImageRep.h: "If sent to a subclass, does this just for the types understood by that subclass.". So, you need only cast result to specified type:

NSBitmapImageRep *imgRep = (NSBitmapImageRep *)[NSBitmapImageRep imageRepWithContentsOfFile:filename];

Note that usually you doesn't need to create NSBitmapImageRep instances yourself, and can use NSImage.