I have two CGImageRef, imgRef1 and imgRef2. I want to create a NSImage with two representations of those CGImageRefs (for creating NSCursor). For this I used BitMapRepresentation initWithCGImage method and then added those representations to NSImage. But this doesn't work.
Then I tried creating NSImage from imageRef1 and imgRef2 and then generated NSData from those NSImage's using TiffRepresentation and then added those representations to finally get the NSImage. But again this is giving low res Image in retina displays. HEre is the sample code: (Any help would be appreciated)
float aHotSpotX = (float)nHotSpotX; float aHotSpotY = (float)nHotSpotY;
NSSize nsSz;
nsSz.width = CGImageGetWidth(nLowImageRef);
nsSz.height = CGImageGetHeight(nLowImageRef);
NSImage* image = [[NSImage alloc] initWithSize:nsSz];
// Could have directly used NSBitmapImage initWithCGImage but some issues with that. Will revisit
NSImage *lImage = CreateNSImageFromCGImage(nLowImageRef);
NSData *lowData = [lImage TIFFRepresentation];
NSBitmapImageRep *lowRep = [NSBitmapImageRep imageRepWithData:lowData];
[image addRepresentation:lowRep];
NSImage *hImage = CreateNSImageFromCGImage(nHiImageRef);
NSData *hiData = [hImage TIFFRepresentation];
NSBitmapImageRep *hiRep = [NSBitmapImageRep imageRepWithData:hiData];
[image addRepresentation:hiRep];
NSCursor* aCursor = [[NSCursor alloc] initWithImage:image hotSpot:NSMakePoint(aHotSpotX, aHotSpotY)];
[image release];
You need to reverse the order. My tests have shown that the higher res image needs to be at index 0 of the TIFF and the lower res image at index 1.
来源:https://stackoverflow.com/questions/12681902/issues-with-nsimage-add-representation-in-retina-display