gpt4 book ai didi

macos - 以编程方式从 1024 * 1024 大小的 NSImage 创建图标 (ICNS)

转载 作者:行者123 更新时间:2023-12-03 17:11:23 27 4
gpt4 key购买 nike

I'm trying to create an ICNS of size 120 * 120 from a 1024x1024 image programmatically. Currently I'm creating an NSImage, then I create CGImageRef objects with the appropriate resolution, finally I'm saving them to a path using CGImageDestinationAddImage().

我浏览了各种链接,其中之一是: Creating an ICNS programmatically: "Unsupported Image Size"

但问题是,代码生成了同样大小的图标文件。 (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);
}

The image used from input path is of 1024 * 1024 pixels. "the correct specification for a 1024-by-1024-pixel element is as 512 points @ 2x. That's a size (of both image and rep) of (NSSize){ 512.0, 512.0 } (points), with the rep being 1024 pixelsWide and 1024 pixelsHigh." NOTE: This has been taken care of but still no success.

最佳答案

我已经完成了这个过程,并做了一些修改就完成了。这是生成大尺寸图像图标的工作代码:

- (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);

}

享受吧:)

关于macos - 以编程方式从 1024 * 1024 大小的 NSImage 创建图标 (ICNS),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25469220/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com