首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >以编程方式创建ICNS:“不支持的图像大小”

以编程方式创建ICNS:“不支持的图像大小”
EN

Stack Overflow用户
提问于 2012-12-29 20:54:56
回答 1查看 998关注 0票数 2

我正在尝试以编程方式创建一个ICNS (包括一个1024x1024映像)。目前我正在创建一个NSImage__,然后创建具有适当分辨率的CGImageRef对象,最后通过使用CGImageDestinationAddImage()__将它们添加到图标中。Peter已经帮我创建了@2x图像,但是图像的大小不想被设置。

这是代码(仍然有点混乱,sourcefile表示到图像的路径):

代码语言:javascript
复制
NSSize sizes[10];
sizes[0] = NSMakeSize(1024,1024);
sizes[1] = NSMakeSize(512,512);
sizes[2] = NSMakeSize(512,512);
sizes[3] = NSMakeSize(256,256);
sizes[4] = NSMakeSize(256,256);
sizes[5] = NSMakeSize(128,128);
sizes[6] = NSMakeSize(64,64);
sizes[7] = NSMakeSize(32,32);
sizes[8] = NSMakeSize(32,32);
sizes[9] = NSMakeSize(16,16);
int count = 0;
for (int i=0 ; i<10 ; i++) {
    if ([[NSUserDefaults standardUserDefaults] boolForKey:[NSString stringWithFormat:@"Size%i",i+1]]) count++;
}
NSURL *fileURL = [NSURL fileURLWithPath:aPath];

// Create icns
CGImageDestinationRef dr = CGImageDestinationCreateWithURL((CFURLRef)fileURL, kUTTypeAppleICNS , count, NULL);
NSImage *img = [[NSImage alloc] initWithContentsOfFile:sourcefile];
for (int i=0 ; i<10 ; i++) {
    if ([[NSUserDefaults standardUserDefaults] boolForKey:[NSString stringWithFormat:@"Size%i",i+1]]) {

        // Create dictionary
        BOOL is2X = true;
        if (i == 1 || i == 3 || i == 5 || i == 7 || i == 9) is2X = false;
        int dpi = 144, size = (int)(sizes[i].width/2);
        if (!is2X) {dpi = 72;size = sizes[i].width;}
        [img setSize:NSMakeSize(size,size)];
        for (NSImageRep *rep in [img representations])[rep setSize:NSMakeSize(size,size)];
        const void *keys[2] = {kCGImagePropertyDPIWidth, kCGImagePropertyDPIHeight};
        const void *values[2] = {CFNumberCreate(0, kCFNumberSInt32Type, &dpi), CFNumberCreate(0, kCFNumberSInt32Type, &dpi)};
        CFDictionaryRef imgprops = CFDictionaryCreate(NULL, keys, values, 2, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);

        // Add image
        NSRect prect = NSMakeRect(0,0,size,size);
        CGImageRef i1 = [img CGImageForProposedRect:&prect context:nil hints:nil];
        CGImageDestinationAddImage(dr, i1, imgprops);
    }
}
CGImageDestinationFinalize(dr);
CFRelease(dr);

size是当前图像的宽度或高度。如果我们制作一个“@2x”图像的话,dpi是144,否则它是72。这些值已经用NSLog检查过了。

生成的ICNS文件中的图像都与输入图像大小相同。如果输入图像的大小为1024x1024,ImageIO会抱怨:

ImageIO:_CGImagePluginWriteICNS不支持的图像大小(1024x1024)-缩放因子:1

每次dpi为72,大小为1024x1024时,都会显示上述错误。

我需要知道如何设置要添加到ICNS文件中的CGImage的大小.

编辑:我记录了图像:

2012年-12:31 12:48:51.281爱康912:680 f\NSImage 0x101b4caf0 Size={512,512} Reps= "NSBitmapImageRep 0x10380b900 Size={512,512} ColorSpace=(尚未加载) BPS=8 BPP=(尚未加载) Pixels=1024x1024 Alpha=YES Planar=NO Format=(尚未加载) CurrentBacking=nil (故障) CGImageSource=0x10380ae70“ )) 2012年-12:31 12:48:52.058爱康912:680 f\NSImage 0x101b4caf0 Size={512,512} Reps= "NSBitmapImageRep 0x10380b900 Size={512,512} ColorSpace=Generic RGB颜色空间BPS=8 BPP=32 Pixels=1024x1024 Alpha=YES Planar=NO Format=2 CurrentBacking=|CGImageRef: 0x101c14630\ CGImageSource=0x10380ae70“ )) 2012年-12:31 12:48:52.111爱康912:680 f\NSImage 0x101b4caf0 Size={256,256} Reps=( "NSBitmapImageRep 0x10380b900 Size={256,256} ColorSpace=Generic RGB颜色空间BPS=8 BPP=32 Pixels=1024x1024 Alpha=YES Planar=NO Format=2 CurrentBacking=|CGImageRef: 0x101c14630\ CGImageSource=0x10380ae70“ )) 2012年-12:31 12:48:52.238爱康912:680 f\NSImage 0x101b4caf0 Size={256,256} Reps=( "NSBitmapImageRep 0x10380b900 Size={256,256} ColorSpace=Generic RGB颜色空间BPS=8 BPP=32 Pixels=1024x1024 Alpha=YES Planar=NO Format=2 CurrentBacking=|CGImageRef: 0x101c14630\ CGImageSource=0x10380ae70“ )) 2012年-12:31 12:48:52.309爱康912:680 f\NSImage 0x101b4caf0 Size={128,128} Reps=( "NSBitmapImageRep 0x10380b900 Size={128,128} ColorSpace=Generic RGB颜色空间BPS=8 BPP=32 Pixels=1024x1024 Alpha=YES Planar=NO Format=2 CurrentBacking=|CGImageRef: 0x101c14630\ CGImageSource=0x10380ae70“ )) 2012年-12:31 12:48:52.409爱康912:680 f\NSImage 0x101b4caf0 Size={128,128} Reps=( "NSBitmapImageRep 0x10380b900 Size={128,128} ColorSpace=Generic RGB颜色空间BPS=8 BPP=32 Pixels=1024x1024 Alpha=YES Planar=NO Format=2 CurrentBacking=|CGImageRef: 0x101c14630\ CGImageSource=0x10380ae70“ )) 2012年-12:31 12:48:52.534爱康912:680 f\NSImage 0x101b4caf0 Size={32,32} Reps=( "NSBitmapImageRep 0x10380b900 Size={32,32} ColorSpace=Generic RGB色空间BPS=8 BPP=32 Pixels=1024x1024 Alpha=YES Planar=NO Format=2 CurrentBacking=|CGImageRef: 0x101c14630\ CGImageSource=0x10380ae70“ )) 2012年-12:31 12:48:52.616爱康912:680 f\NSImage 0x101b4caf0 Size={32,32} Reps=( "NSBitmapImageRep 0x10380b900 Size={32,32} ColorSpace=Generic RGB色空间BPS=8 BPP=32 Pixels=1024x1024 Alpha=YES Planar=NO Format=2 CurrentBacking=|CGImageRef: 0x101c14630\ CGImageSource=0x10380ae70“ )) 2012年-12:31 12:48:52.729爱康912:680 f\NSImage 0x101b4caf0 Size={16,16} Reps=( "NSBitmapImageRep 0x10380b900 Size={16,16} ColorSpace=Generic RGB色空间BPS=8 BPP=32 Pixels=1024x1024 Alpha=YES Planar=NO Format=2 CurrentBacking=|CGImageRef: 0x101c14630\ CGImageSource=0x10380ae70“ )) 2012年-12:31 12:48:52.864爱康912:680 f\NSImage 0x101b4caf0 Size={16,16} Reps=( "NSBitmapImageRep 0x10380b900 Size={16,16} ColorSpace=Generic RGB色空间BPS=8 BPP=32 Pixels=1024x1024 Alpha=YES Planar=NO Format=2 CurrentBacking=|CGImageRef: 0x101c14630\ CGImageSource=0x10380ae70“ ))

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-12-30 02:01:56

错误信息是正确的。您正在输入不受IconFamily格式支持的大小的图像。具体来说,从您的输出:

Size={11.52,11.52} Reps=( "NSBitmapImageRep 0x10421fc30 Size={11.52,11.52} ColorSpace=(尚未加载) BPS=8 BPP=(尚未加载) Pixels=1024x1024 Alpha=NO Planar=NO Format=(尚未加载) CurrentBacking=nil (未加载) CGImageSource=0x104221170“

对于IconFamily的任何元素,11.52点都不是有效的大小。你需要弄清楚为什么这个图像和代表有那么大的尺寸。

还有几件事:

  1. 正如我在另一个答案上告诉你的,你不需要改变表示的像素大小。不要使用像素大小。设置rep和图像的size (点大小)(最好设置为有效的)。
  2. -[NSImage initWithSize:]文档说: 允许通过传递(0.0,0.0)大小来初始化接收器;但是,在使用NSImage对象之前,必须将接收器的大小设置为非零值,否则会引发异常。

您没有设置任何一个对象的大小,这是您需要做的。(我很惊讶你没有得到像文档承诺那样的例外。)

正如我在另一个问题上提到的,不再存在1024点元素;1024×1024像素元素的正确规范是512点@ 2x。这是一个size (图像和rep)的(NSSize){ 512.0, 512.0 } (点数),其rep为1024 pixelsWide和1024 pixelsHigh

看来我之前错过了一种关键成分。这就是了。

您提供给CGImageDestination的CGImageDestination没有与其相关的点大小--只有NSImages和NSImageReps有。CGImage只具有像素大小;没有指示图像的物理大小或分辨率。

要告诉CGImageDestination给定的CGImage是为@ 1x还是@ 2x,您需要创建一个字典,给出图像的DPI:

代码语言:javascript
复制
NSDictionary *imageProps1x = @{
    (__bridge NSString *)kCGImagePropertyDPIWidth: @72.0,
    (__bridge NSString *)kCGImagePropertyDPIHeight: @72.0,
};
NSDictionary *imageProps2x = @{
    (__bridge NSString *)kCGImagePropertyDPIWidth: @144.0,
    (__bridge NSString *)kCGImagePropertyDPIHeight: @144.0,
};

将正确的字典作为最后一个参数传递给CGImageDestinationAddImage

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14085843

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档