psd Image creation with layer properties using CGI

2019-08-18 01:53发布

问题:

Working in Mac OSX, Cocoa

I have an psd image with layered property. I want to crop it to the crop rect and save this cropped image with the settings of original image.

I am using CGImageRef for all the image related operations.

I have enclosed the code i used to crop the image is given below. But it fails to create the layered image.

NSImage *img = [[NSImage alloc]initWithContentsOfFile:imagePath];
NSBitmapImageRep *rep = [[NSBitmapImageRep alloc] initWithData:[img TIFFRepresentation]];
CGImageRef imageRef = [rep CGImage];
CGImageRef cropedImage = CGImageCreateWithImageInRect(imageRef, cropRect);


CGImageDestinationRef idst = CGImageDestinationCreateWithURL( url, type, 1, NULL );
if( idst != NULL ) {

           CGImageDestinationAddImage( idst, image, properties );
           bool success = CGImageDestinationFinalize( idst );

    }

回答1:

There's no way to read separate layers from PSD files in Mac OS X with only built-in APIs. Everything is almost certainly going to go through CGImageSource, and CGImageSource does not support reading separate layers from PSD files. (The documentation says this explicitly for the CGImageSourceGetCount function, and experimentation using only CGImageSourceCreateImageAtIndex bears it out.)

You'll need to use a third-party PSD-reading library.