首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从1024 * 1024大小NSImage以编程方式创建图标

从1024 * 1024大小NSImage以编程方式创建图标
EN

Stack Overflow用户
提问于 2014-08-24 06:49:42
回答 2查看 702关注 0票数 2

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

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

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

下面是代码:

代码语言:javascript
复制
- (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。注:这已得到处理,但仍未取得成功。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-08-25 05:26:24

我已经经历过这件事,而且几乎没有什么变化就完成了。下面是生成大尺寸图像的图标的工作代码:

代码语言:javascript
复制
- (void)generateIconSizeFromImage:(NSString *)path

{

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2014-08-26 07:52:59

绘制rect方法将适用于此。下面是示例代码:

代码语言:javascript
复制
[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));
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25469220

复制
相关文章

相似问题

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