如何以编程方式创建ICNS图标? [英] How to create a ICNS icon programmatically?

查看:270
本文介绍了如何以编程方式创建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);
}

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


ImageIO:CGImageDestinationFinalize图片目的地
没有足够的图片


我的代码有什么问题?






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


解决方案

您可以使用 IconFamily


IconFamily是Mac OS X Carbon的Cocoa / Objective-C包装
API的图标系列数据类型。它的主要目的是使Cocoa
应用程序能够从NSImage
实例轻松创建自定义文件图标,从而利用Mac OS X的高分辨率RGBA
缩略图图标格式提供丰富详细缩略图预览
的文件内容。




  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 :

ImageIO: CGImageDestinationFinalize image destination does not have enough images

What's wrong with my code?


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

解决方案

You can use IconFamily.

IconFamily is a Cocoa/Objective-C wrapper for the Mac OS X Carbon API's "icon family" data type. Its main purpose is to enable Cocoa applications to easily create custom file icons from NSImage instances, and thus take advantage of Mac OS X's high-resolution RGBA "thumbnail" icon formats to provide richly detailed thumbnail previews of the files' contents.

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

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆