4

I am setting NSImages to the contents of several CALayers. All of these images have 4 reps, 1x, 2x, 5x and 10x. 3 of these images scale seemingly fine when I set the contentsScale when my view changes it's backingScaleFactor but one of them seems to always choose the 1x representation no matter what I do. Here are my images logged into console:

<NSImage 0x1019cea60 Size={20, 24} Reps=(
    "NSBitmapImageRep 0x101a0db60 Size={20, 24} ColorSpace=(not yet loaded) BPS=8 BPP=(not yet loaded) Pixels=20x24 Alpha=YES Planar=NO Format=(not yet loaded) CurrentBacking=nil (faulting) CGImageSource=0x101a0c8e0",
    "NSBitmapImageRep 0x101a0ec90 Size={20, 24} ColorSpace=(not yet loaded) BPS=8 BPP=(not yet loaded) Pixels=40x48 Alpha=YES Planar=NO Format=(not yet loaded) CurrentBacking=nil (faulting) CGImageSource=0x101a0dbd0",
    "NSBitmapImageRep 0x101a0fdb0 Size={20, 24} ColorSpace=(not yet loaded) BPS=8 BPP=(not yet loaded) Pixels=100x120 Alpha=YES Planar=NO Format=(not yet loaded) CurrentBacking=nil (faulting) CGImageSource=0x101a0ed00",
    "NSBitmapImageRep 0x101a10ef0 Size={20, 24} ColorSpace=(not yet loaded) BPS=8 BPP=(not yet loaded) Pixels=200x240 Alpha=YES Planar=NO Format=(not yet loaded) CurrentBacking=nil (faulting) CGImageSource=0x101a0fcb0"
)>
<NSImage 0x1001faa90 Size={28, 320} Reps=(
    "NSBitmapImageRep 0x101a06240 Size={28, 320} ColorSpace=(not yet loaded) BPS=8 BPP=(not yet loaded) Pixels=28x320 Alpha=YES Planar=NO Format=(not yet loaded) CurrentBacking=nil (faulting) CGImageSource=0x101a05230",
    "NSBitmapImageRep 0x101a07370 Size={28, 320} ColorSpace=(not yet loaded) BPS=8 BPP=(not yet loaded) Pixels=56x640 Alpha=YES Planar=NO Format=(not yet loaded) CurrentBacking=nil (faulting) CGImageSource=0x101a062d0",
    "NSBitmapImageRep 0x101a08490 Size={28, 320} ColorSpace=(not yet loaded) BPS=8 BPP=(not yet loaded) Pixels=140x1600 Alpha=YES Planar=NO Format=(not yet loaded) CurrentBacking=nil (faulting) CGImageSource=0x101a07400",
    "NSBitmapImageRep 0x101a095c0 Size={28, 320} ColorSpace=(not yet loaded) BPS=8 BPP=(not yet loaded) Pixels=280x3200 Alpha=YES Planar=NO Format=(not yet loaded) CurrentBacking=nil (faulting) CGImageSource=0x101a08390"
)>
<NSImage 0x10052e790 Size={28, 40} Reps=(
    "NSBitmapImageRep 0x101a01d10 Size={28, 40} ColorSpace=(not yet loaded) BPS=8 BPP=(not yet loaded) Pixels=28x40 Alpha=YES Planar=NO Format=(not yet loaded) CurrentBacking=nil (faulting) CGImageSource=0x101a005d0",
    "NSBitmapImageRep 0x101a02de0 Size={28, 40} ColorSpace=(not yet loaded) BPS=8 BPP=(not yet loaded) Pixels=56x80 Alpha=YES Planar=NO Format=(not yet loaded) CurrentBacking=nil (faulting) CGImageSource=0x101a01da0",
    "NSBitmapImageRep 0x101a03f00 Size={28, 40} ColorSpace=(not yet loaded) BPS=8 BPP=(not yet loaded) Pixels=140x200 Alpha=YES Planar=NO Format=(not yet loaded) CurrentBacking=nil (faulting) CGImageSource=0x101a02fa0",
    "NSBitmapImageRep 0x101a05080 Size={28, 40} ColorSpace=(not yet loaded) BPS=8 BPP=(not yet loaded) Pixels=280x400 Alpha=YES Planar=NO Format=(not yet loaded) CurrentBacking=nil (faulting) CGImageSource=0x101a040c0"
)>
<NSImage 0x101bbfc40 Size={28, 600} Reps=(
    "NSBitmapImageRep 0x101a0a760 Size={28, 600} ColorSpace=(not yet loaded) BPS=8 BPP=(not yet loaded) Pixels=28x600 Alpha=YES Planar=NO Format=(not yet loaded) CurrentBacking=nil (faulting) CGImageSource=0x101a094f0",
    "NSBitmapImageRep 0x101a0b890 Size={28, 600} ColorSpace=(not yet loaded) BPS=8 BPP=(not yet loaded) Pixels=56x1200 Alpha=YES Planar=NO Format=(not yet loaded) CurrentBacking=nil (faulting) CGImageSource=0x101a0a7f0",
    "NSBitmapImageRep 0x101a0ba60 Size={28, 600} ColorSpace=(not yet loaded) BPS=8 BPP=(not yet loaded) Pixels=140x3000 Alpha=YES Planar=NO Format=(not yet loaded) CurrentBacking=nil (faulting) CGImageSource=0x101a0ab10",
    "NSBitmapImageRep 0x101a0c9b0 Size={28, 600} ColorSpace=(not yet loaded) BPS=8 BPP=(not yet loaded) Pixels=280x6000 Alpha=YES Planar=NO Format=(not yet loaded) CurrentBacking=nil (faulting) CGImageSource=0x101a0b920"
)>

The third one (28 x 40) is always being drawn at 1x. Here is the code I use to set the scale factor:

- (void)viewDidChangeBackingProperties {
    [super viewDidChangeBackingProperties];
    self.layer.contentsScale       = self.window.backingScaleFactor;
    self.otherLayer.contentsScale  = self.window.backingScaleFactor;

    self.otherLayer.contents = self.image;
}

I set the layer contents to image again because in this question it was established that the CALayer chooses the best representation of the NSImage when it draws and it would need to be set again.

I ran the new -recommendedLayerContentsScale: method on all of my images and they all returned 2.0 when given 2.0 except for the one that is not scaling correctly.

EDIT: I wrote the broken image to a file and then opened made a new NSImage with -initWithContentsOfFile in a different app and I put it in an NSImageView and the problem persists! It works with one of the images that were working though. Clearly something is wrong with Apple's image choosing code.

Community
  • 1
  • 1
Alex Zielenski
  • 3,591
  • 1
  • 26
  • 44

0 Answers0