1

我正在尝试以编程方式从 1024x1024 图像创建大小为 120 * 120 的 ICNS。目前我正在创建一个 NSImage,然后创建具有适当分辨率的 CGImageRef 对象,最后我使用 CGImageDestinationAddImage() 将它们保存到路径中。

我浏览了各种链接,其中一个是: 以编程方式创建 ICNS:“不支持的图像大小”

但问题是,代码生成了相同大小的图标文件。(1024 * 1024)

这是代码:

- (void)generateIconSizeFromImage:(NSString *)path
{
    //destination path

    NSURL *fileURL = [NSURL fileURLWithPath:@"/Users/harjotsingh/Desktop/TestIconGenerated/test1@2x.png"];
    CGImageDestinationRef dr = CGImageDestinationCreateWithURL((__bridge CFURLRef)fileURL, kUTTypePNG , 1, NULL);

    //image from path
    NSImage *img = [[NSImage alloc] initWithContentsOfFile:path];

    //setting its size to one of icon size
    [img setSize:NSMakeSize(60,60)];

    //setting its rep size to one of icon size
    for (NSImageRep *rep in [img representations])[rep setSize:NSMakeSize(60,60)];

    //setting the dpi, so it create 120 * 120 size icon file
    NSDictionary *imageProps1x = @{
                                   (__bridge NSString *)kCGImagePropertyDPIWidth: @144.0,
                                   (__bridge NSString *)kCGImagePropertyDPIHeight: @144.0,
                                   };

    // Add rect size
    NSRect prect = NSMakeRect(0,0,60,60);

    //get image for desired size
    CGImageRef generatedImage = [img CGImageForProposedRect:&prect context:[NSGraphicsContext currentContext] hints:nil];

    //sadly it shows the same 1024 * 1024 size
    NSLog(@"width:-- %zu && height:-- %zu",CGImageGetWidth(generatedImage),CGImageGetHeight(generatedImage));

    //adding to destination folder
    CGImageDestinationAddImage(dr, generatedImage, (__bridge CFDictionaryRef)(imageProps1x));

    //finalize.
    CGImageDestinationFinalize(dr);

    CFRelease(dr);
}

输入路径使用的图像为 1024 * 1024 像素。“1024 x 1024 像素元素的正确规格是 512 点 @ 2x。这是 (NSSize){ 512.0, 512.0 } (点)的大小(图像和代表),代表为 1024 像素宽和 1024 像素高。” 注意:这已被处理,但仍然没有成功。

4

2 回答 2

1

我已经完成了这个并通过一些更改完成了它。这是生成图标形式大尺寸图像的工作代码:

- (void)generateIconSizeFromImage:(NSString *)path

{

NSImage * smallImage = [[NSImage alloc] initWithContentsOfFile:path];
[smallImage setSize:NSMakeSize(120,120)];

[smallImage lockFocus];

[[NSGraphicsContext currentContext] setImageInterpolation:NSImageInterpolationHigh];

[smallImage drawInRect:CGRectMake(0, 0, 120,120)];

[smallImage unlockFocus];


//destination path
NSURL *fileURL = [NSURL fileURLWithPath:@"/Volumes/Harjot/Documents/MyProjects/Cobby Mac App/test60@2x.png"];

CGImageDestinationRef dr = CGImageDestinationCreateWithURL((__bridge CFURLRef)fileURL, kUTTypePNG , 1, NULL);

//setting the dpi, so it create 120 * 120 size icon file
NSDictionary *imageProps1x = @{
                               (__bridge NSString *)kCGImagePropertyDPIWidth: @144.0,
                               (__bridge NSString *)kCGImagePropertyDPIHeight: @144.0,
                               };

//get image for desired size
CGImageRef generatedImage = [smallImage CGImageForProposedRect:NULL context:[NSGraphicsContext currentContext] hints:nil];

//now it works and generate the 120 by 120 icon
NSLog(@"width:-- %zu && height:-- %zu",CGImageGetWidth(generatedImage),CGImageGetHeight(generatedImage));

//adding to destination folder
CGImageDestinationAddImage(dr, generatedImage, (__bridge CFDictionaryRef)(imageProps1x));

//finalize.
CGImageDestinationFinalize(dr);

CFRelease(dr);

}

享受 :)

于 2014-08-25T05:26:24.797 回答
0

draw rect 方法将适用于此。这是示例代码:

[smallImage drawInRect:CGRectMake(0, 0, 120,120)]


//destination path
NSURL *fileURL = [NSURL fileURLWithPath:@“Your saving path…];

CGImageDestinationRef dr = CGImageDestinationCreateWithURL((__bridge CFURLRef)fileURL, kUTTypePNG , 1, NULL);

//setting the dpi, so it create 120 * 120 size icon file
NSDictionary *imageProps1x = @{
                               (__bridge NSString *)kCGImagePropertyDPIWidth: @144.0,
                               (__bridge NSString *)kCGImagePropertyDPIHeight: @144.0,
                               };

//get image for desired size
CGImageRef generatedImage = [smallImage CGImageForProposedRect:NULL context:[NSGraphicsContext currentContext] hints:nil];

//now it works and generate the 120 by 120 icon
NSLog(@"width:-- %zu && height:-- %zu",CGImageGetWidth(generatedImage),CGImageGetHeight(generatedImage));

//adding to destination folder
CGImageDestinationAddImage(dr, generatedImage, (__bridge CFDictionaryRef)(imageProps1x));
于 2014-08-26T07:52:59.227 回答