使用 CGImageRef 创建具有图层属性的 psd 图像
在 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 );
}
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 );
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
仅使用内置 API 无法从 Mac OS X 中的 PSD 文件中读取单独的图层。几乎可以肯定,所有内容都会经过 CGImageSource,而 CGImageSource 不支持从 PSD 文件中读取单独的图层。 (文档明确说明了
CGImageSourceGetCount
函数,并且仅使用CGImageSourceCreateImageAtIndex
进行的实验证实了这一点。)您需要使用第三方 PSD - 阅读图书馆。
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 onlyCGImageSourceCreateImageAtIndex
bears it out.)You'll need to use a third-party PSD-reading library.