将 NSImage 转换为 CIImage 而不会降低质量
我正在尝试将 NSImage 转换为 CIImage。当我这样做时,图像质量似乎有很大的损失。我认为这是因为“TIFFRepresentation”。有人有更好的方法吗?多谢。
NSImage *image = [[NSImage alloc] initWithData:[someSource dataRepresentation]];
NSData * tiffData = [image TIFFRepresentation];
CIImage *backgroundCIImage = [[CIImage alloc] initWithData:tiffData];
CIContext *ciContext = [[NSGraphicsContext currentContext] CIContext];
[ciContext drawImage:backgroundCIImage atPoint:CGPointZero fromRect:someRect];
I am trying to convert an NSImage to a CIImage. When I do this, there seems to be a huge loss in image quality. I think it is because of the "TIFFRepresentation". Does anyone have a better method? Thanks a lot.
NSImage *image = [[NSImage alloc] initWithData:[someSource dataRepresentation]];
NSData * tiffData = [image TIFFRepresentation];
CIImage *backgroundCIImage = [[CIImage alloc] initWithData:tiffData];
CIContext *ciContext = [[NSGraphicsContext currentContext] CIContext];
[ciContext drawImage:backgroundCIImage atPoint:CGPointZero fromRect:someRect];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的问题确实是转换为 TIFF。 PDF 是一种矢量格式,而 TIFF 是位图格式,因此 TIFF 在较大尺寸下会显得模糊。
您最好的选择可能是从 NSImage 获取 CGImage 并从中创建 CIImage。或者直接从原始数据创建 CIImage。
Your problem is indeed converting to TIFF. PDF is a vector format, while TIFF is bitmap, so a TIFF will look blurry at larger sizes.
Your best bet is probably to get a CGImage from the NSImage and create the CIImage from that. Either that or just create the CIImage from the original data.
尝试将该行替换
为
因为 文档 指出 TIFFRepresentation 使用与每个图像表示关联的 TIFF 压缩选项,该选项可能不是 NSTIFFCompressionNone。因此,您应该明确希望 tiffData 未压缩。
Try replacing the line
with
because the documentation states that TIFFRepresentation uses the TIFF compression option associated with each image representation, which might not be NSTIFFCompressionNone. Thus, you should be explicit about wanting the tiffData uncompressed.
我终于解决了这个问题。基本上,我在屏幕外渲染 pdf 文档的正常分辨率的两倍,然后捕获视图显示的图像。要获得更详细的图像,只需增加缩放系数即可。请参阅下面的代码以获取概念证明。我没有显示 CIImage,但是一旦获得位图,只需使用 CIImage 方法从位图创建 CIImage 即可。
I finally solved the problem. Basically, I render the pdf document two times its normal resolution offscreen and then capture the image displayed by the view. For a more detailed image, just increase the scaling factor. Please see the code below for the proof of concept. I didn't show the CIImage but once you get the bitmap, just use the CIImage method to create the CIImage from the bitmap.