生成Xcode中各个尺寸的Mac App Icon

在Mac App都有一个图标, 在Assets.xcassets/AppIcon中需要的像素尺寸很多,

有些网页上一次只能生成一个尺寸, 还要选来选去的, 太麻烦了,  本来想省事, 结果还不如自己写个来的方便, 也不复杂!

#import <Foundation/Foundation.h>
#import <Cocoa/Cocoa.h>

void saveImage_png(NSImage *image,NSString *name){
    NSData *data = [image TIFFRepresentation];
    CGImageSourceRef source = CGImageSourceCreateWithData((CFDataRef)data, NULL);
    CGImageRef cgimage = CGImageSourceCreateImageAtIndex(source, 0, NULL);
    NSBitmapImageRep *rep = [[NSBitmapImageRep alloc] initWithCGImage:cgimage];
    NSData *pngData = [rep representationUsingType:NSBitmapImageFileTypePNG properties:@{}];
    [pngData writeToFile:name atomically:NO];
}


NSImage * zoom(NSImage *image,CGFloat width){
    NSImage *smallImage = [[NSImage alloc] initWithSize: CGSizeMake(width, width)];
    [smallImage lockFocus];
    [image setSize: CGSizeMake(width, width)];
    [[NSGraphicsContext currentContext] setImageInterpolation:NSImageInterpolationHigh];
    [image drawAtPoint:NSZeroPoint fromRect:CGRectMake(0, 0, width, width) operation:NSCompositingOperationCopy fraction:1.0];
    [smallImage unlockFocus];
    return smallImage;
}


int main(int argc, const char * argv[]) {
    @autoreleasepool {
        if (argc!=2){
            printf("Just Need 1 argument: image_file_path\n");
            return -1;
        }
        NSString *toolpath = [NSString stringWithUTF8String:argv[0]];
        NSURL *toolURL = [NSURL URLWithString:toolpath];
        NSString *toolName = [toolURL lastPathComponent];
        NSString *file = [NSString stringWithUTF8String:argv[1]];
        if ([file isEqualToString:@"-h"]) {
            printf("Usage: %s Image_file_path\n",toolName.UTF8String);
            return 0;
        }
        if (![[NSFileManager defaultManager] fileExistsAtPath:file]) {
            printf("the file don't exists:%s\n",file.UTF8String);
            return -2;
        }
        NSURL *fileURL = [NSURL URLWithString:file];
        NSString *path = [fileURL URLByDeletingLastPathComponent].path;
        NSString *name=[[fileURL URLByDeletingPathExtension] lastPathComponent];
        NSImage *image = [[NSImage alloc] initWithContentsOfFile:file];
        for (int i=8; i<1024; i=i*2) {
            NSImage *small = zoom(image, i);
            NSString *savePath = [NSString stringWithFormat:@"%@/%@_%dx%d.png",path,name,i*2,i*2];
            NSURL *saveURL = [NSURL URLWithString:savePath];
            printf("create: %s\n",saveURL.lastPathComponent.UTF8String);
            saveImage_png(small, savePath);
        }
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

auspark

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值