在 Mac OSX、Cocoa 中工作
我有一个具有分层属性的 psd 图像。 我想将其裁剪到裁剪矩形并使用原始图像的设置保存裁剪后的图像。
我使用 CGImageRef 进行所有与图像相关的操作。
我已附上用于裁剪图像的代码,如下所示。但它无法创建分层图像。
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 );
}
最佳答案
仅使用内置 API 无法从 Mac OS X 中的 PSD 文件中读取单独的图层。几乎可以肯定,所有内容都会经过 CGImageSource,而 CGImageSource 不支持从 PSD 文件中读取单独的图层。 ( The documentation says this explicitly for the CGImageSourceGetCount
function ,仅使用 CGImageSourceCreateImageAtIndex 的实验证明了这一点。)
您需要使用第三方 PSD 读取库。
关于cocoa - 使用 CGImageRef 创建具有图层属性的 psd 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3456560/