我试图通过编程方式从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 }(点)的大小(图像和rep),rep为1024 pixelsWide和1024 pixelsHigh。注:这已得到处理,但仍未取得成功。
发布于 2014-08-25 05:26:24
我已经经历过这件事,而且几乎没有什么变化就完成了。下面是生成大尺寸图像的图标的工作代码:
- (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-26 07:52:59
绘制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));https://stackoverflow.com/questions/25469220
复制相似问题