NSImage initWithContentsOfURL missing alpha channel when loading TIFFs

696 views Asked by At

In my app I use initWithContentsOfURL to load various types of image (JPEG, TIFF, PNG, GIF, etc) into an image, and then into an OpenGL texture.

The only type that loads an image with an alpha channel is png. (in the list above, only PNG and TIFF can contain alpha data.) If I try to load a .tiff image, it gets loaded without an alpha channel (the image's image rep reports alpha=NO, and it reports bitsPerPixel of 24.

I can edit an image with alpha in PS, save it as a PNG and a TFF, and the PNG loads in my program with alpha but the TIFF does not. Further, I can open the TIFF image in PS and confirm that it does have alpha data.

What am I missing here? Why are my TIFF images not loading with an alpha channel? And is there another appkit call I can make that WILL load my TIFF without dropping the alpha channel on the floor?

EDIT:

Since posting this question I've found that some 4-channel TIFFs load with alpha data and some do not. I have not yet figured out what workflow results in the different results.

This file loads with an alpha channel in Photoshop, but not if you load it in Cocoa using -[[NSImage alloc] initWithContentsOfURL]:

Image "Red Julia Seahorse crop"

A similar image that also has an alpha channel DOES load with alpha using the above Cocoa call:

Image "Transparent Seahorses"

2

There are 2 answers

2
picciano On BEST ANSWER

The issue is definitely with how the images were saved, most likely the TIFF options used.

Using your two images, the "Red Julia Seahorse Crop" image did not display with transparency, but the "Transparent Seahorses" displayed correctly with transparency.

I opened the "Red Julia Seahorse Crop" in Photoshop and re-saved the image (unchanged), but made sure the "Save Transparency" check box was selected in the "TIFF Options" dialog box. Once saved, that image now showed transparency correctly in the application.

screen shot TIFF options screen shot output from application

1
John Cook On

I just tried (OSX 10.9.4) and the image loaded complete with graduated transparency. The code I used is trivial:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    // Insert code here to initialize your application
    NSURL *imageURL = [NSURL URLWithString: @"file:/Users/john/Desktop/test.tiff"];
    NSImage *image = [[NSImage alloc] initWithContentsOfURL: imageURL];
    self.imageView.image = image;
}

I created a TIFF using layers and without layers (two tests). Both worked. I tried a couple of different NSImageView backgrounds to verify the graduated transparency was genuine. (If you choose different Image View border styles in IB the background colour changes too).

I used Photoshop 12 (CS5) to create the images, and manually checked the 'Save Transparency' checkbox in the TIFF Options dialog when saving.

Hopefully something here helps you home in on your issue. From my tests it all works as expected.