2017-07-10 2 views
0

J'ai essayé d'utiliser How to load JPG file into NSBitmapImageRep? pour répondre à ma question, mais je reçois:Comment charger un JPEG dans une matrice d'image bitmap?

"types de pointeur incompatibles initialisation 'nsbitmapimagerep *' avec une expression de type 'nsimagerep *'". en ce qui concerne:

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

Répondre

1

Le compilateur a raison. Les representions d'un NSImage sont un NSArray de NSImageRep (voir la documentation):

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

Le message du compilateur est seulement un avertissement, si vous pourriez lancer la deuxième ligne et utiliser la voie directe (sans utiliser NSImage) pour obtenir une NSBitmapImageRep:

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

une autre façon d'éviter le (un peu laid coulée) est:

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

Attention: cette version n'évalue PAS @ 2 etc. dans le fichier cheminPath!

0

NSBitmapImageRep est une sous-classe de NSImageRep, qui est "semiabstract". NSImage peut également stocker d'autres types de représentations - comme NSEPSImageRep, pour les images EPS.

Si vous êtes sûr que c'est un bitmap, vous devriez pouvoir le lancer.