本文介绍了如何以编程方式创建ICNS图标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

OK这是我想要的:




  • 取一些 NSImage s

  • 将它们添加到 ICNS 文件

  • 保存



这是我到目前为止做的(纯粹作为测试):

   - (CGImageRef)refFromImage:(NSImage *)img 
{
CGImageSourceRef source;

source = CGImageSourceCreateWithData((CFDataRef)[img TIFFRepresentation],NULL);
CGImageRef maskRef = CGImageSourceCreateImageAtIndex(source,0,NULL);

return maskRef;
}

- (void)awakeFromNib
{
NSImage * img1 = [NSImage imageNamed:@image1];
NSImage * img2 = [NSImage imageNamed:@image2];

NSLog(@%@,img1);
CGImageRef i1 = [self refFromImage:img1];
CGImageRef i2 = [self refFromImage:img2];

NSURL * fileURL = [NSURL fileURLWithPath:[@〜/ Documents / final.icnsstringByExpandingTildeInPath]];
CGImageDestinationRef dr = CGImageDestinationCreateWithURL((__ bridge CFURLRef)fileURL,kUTTypeAppleICNS,1,NULL);

CGImageDestinationAddImage(dr,i1,NULL);
CGImageDestinationAddImage(dr,i2,NULL);
/ *甚至尝试添加'multiple'次

CGImageDestinationAddImage(dr,i1,NULL);
CGImageDestinationAddImage(dr,i2,NULL);
CGImageDestinationAddImage(dr,i1,NULL);
CGImageDestinationAddImage(dr,i2,NULL);

* /

CGImageDestinationFinalize(dr);

CFRelease(dr);
}

但是,它仍然会发生错误:

我的代码有什么问题?






我已经看过下面的答案, / p>





解决方案

您可以使用。



  NSImage * mImage = [NSImage alloc] initWithContentsOfFile:@/ Users / Username / Desktop / WhiteTiger.jpg]; 
IconFamily * fam = [IconFamily iconFamilyWithThumbnailsOfImage:mImage];
[fam writeToFile:@/ Users / Username / Desktop / WhiteTiger.icns];


OK this is what I want :

  • Take some NSImages
  • Add them to an ICNS file
  • Save it

This is what I've done so far (purely as a test) :

- (CGImageRef)refFromImage:(NSImage*)img
{
    CGImageSourceRef source;

    source = CGImageSourceCreateWithData((CFDataRef)[img TIFFRepresentation], NULL);
    CGImageRef maskRef =  CGImageSourceCreateImageAtIndex(source, 0, NULL);

    return maskRef;
}

- (void)awakeFromNib
{
    NSImage* img1 = [NSImage imageNamed:@"image1"];
    NSImage* img2 = [NSImage imageNamed:@"image2"];

    NSLog(@"%@",img1);
    CGImageRef i1 = [self refFromImage:img1];
    CGImageRef i2 = [self refFromImage:img2];

    NSURL *fileURL = [NSURL fileURLWithPath:[@"~/Documents/final.icns" stringByExpandingTildeInPath]];
    CGImageDestinationRef dr = CGImageDestinationCreateWithURL((__bridge CFURLRef)fileURL, kUTTypeAppleICNS , 1, NULL);

    CGImageDestinationAddImage(dr, i1, NULL);
    CGImageDestinationAddImage(dr, i2, NULL);
    /* Even tried adding 'multiple' times

    CGImageDestinationAddImage(dr, i1, NULL);
    CGImageDestinationAddImage(dr, i2, NULL);
    CGImageDestinationAddImage(dr, i1, NULL);
    CGImageDestinationAddImage(dr, i2, NULL);

    */

    CGImageDestinationFinalize(dr);

    CFRelease(dr);
}

But, it still keeps throwing an error :

What's wrong with my code?


I've had a look at the answers below, but still nothing :

解决方案

You can use IconFamily.

NSImage *mImage = [[NSImage alloc] initWithContentsOfFile:@"/Users/Username/Desktop/WhiteTiger.jpg"];
IconFamily *fam = [IconFamily iconFamilyWithThumbnailsOfImage:mImage];
[fam writeToFile:@"/Users/Username/Desktop/WhiteTiger.icns"];

这篇关于如何以编程方式创建ICNS图标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-18 07:30