我正在尝试以编程方式从1024x1024图像创建大小为120 * 120的ICNS。目前我正在创建一个NSImage,然后我用适当的分辨率创建CGImageRef对象,最后我用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); }
输入路径使用的图像为1024 * 1024像素。 “1024×1024像素元素的正确规格为512点@ 2x。这是(NSSize){512.0,512.0}(点)的大小(图像和代表),代表是1024像素宽和1024像素高。“ 注意:这已经得到了解决,但仍然没有成功。
如有任何关于我的问题的疑问,请与我联系。
感谢您的帮助。
答案 0 :(得分: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);
}
享受:)
答案 1 :(得分: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));