首页 > 解决方案 > 使用 Objective-C 访问 .heic 图像文件的深色版本

问题描述

在 MacOS 上,.heic 图像文件可以包含一个浅色和一个深色版本。这可以通过系统壁纸看到open:例如 open /System/Library/Desktop\ Pictures/Dome.heic

预览应用程序显示两个版本。我的目标是能够使用 Objective-C 访问它们,因为我想将浅色和深色版本都保存为 .jpg 格式。

以下代码段成功提取了轻量版本并将其保存到磁盘:

NSString* wallpaper_path = /* insert source path here */;
NSString* converted_path_str = /* insert destination path here */;

NSImage* image = [[[NSImage alloc] initWithContentsOfFile:wallpaper_path] autorelease];
NSData* data = [image TIFFRepresentation];
NSBitmapImageRep* rep = [NSBitmapImageRep imageRepWithData:data];
NSDictionary* properties =[NSDictionary dictionaryWithObject:[NSNumber numberWithFloat:1.0] forKey:NSImageCompressionFactor];
data = [rep representationUsingType:NSJPEGFileType properties:properties];
[data writeToFile:converted_path_str atomically:NO];

但是我找不到如何使用NSImage,NSImageRepNSBitmapImageRep. 由于预览成功地做到了,我认为这是可能的。我只是不知道该怎么做。

标签: objective-cimageheic

解决方案


推荐阅读