下载Icons8的图标

Icons8的图标都挺不错的,不过只支持100x100的png保存。不过抓了一下API之后,发现可以直接拿SVG。省去了一年几百美元的VIP费。。。

代码如下:

//
//  main.m
//  icons8
//
//  Created by BlueCocoa on 15/4/19.
//  Copyright (c) 2015年 0xBBC. All rights reserved.
//
 
#import <Foundation/Foundation.h>
#import <stdlib.h>

int main(int argc, const char * argv[]){
    @autoreleasepool {
        // IconNumber WhereToSave
        if (argc != 3) return 0;
        NSError * error = nil;
        NSDictionary * json = [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfURL:[NSURLURLWithString:[NSString stringWithFormat:@"http://api.icons8.com/api/iconsets/v2/icon?id=%lld&format=json&files=svg&info=enabled",atoll(argv[1])]]] options:kNilOptions error:&error];
        if (!error) {
            NSDictionary * result = [json valueForKey:@"result"];
            if (result) {
                NSArray * icons = [result valueForKey:@"icons"];
                for (NSDictionary * icon in icons) {
                    NSString * svgInBase64 = [icon valueForKey:@"svg"];
                    NSData * svgBase64Data = [[NSData alloc] initWithBase64EncodedString:svgInBase64  options:NSDataBase64DecodingIgnoreUnknownCharacters];
                    NSString * svg = [[NSString alloc] initWithData:svgBase64Data encoding:NSUTF8StringEncoding];
                    [svg writeToFile:[NSString stringWithCString:argv[2] encoding:NSUTF8StringEncoding] atomically:YES encoding:NSUTF8StringEncoding error:&error];
                    if (error)
                        printf("[ERROR ON SAVE] %s\n",[[error localizedFailureReason] UTF8String]);
                }
            } else {
                NSArray * errors = [json valueForKey:@"errors"];
                for (NSDictionary * error in errors)
                    printf("[ERROR ON LOAD] %s\n",[(NSString *)[error valueForKey:@"code"] UTF8String]);
            }
        }
    }
    return 0;
}
声明: 本文为0xBBC原创, 转载注明出处喵~

发表评论

电子邮件地址不会被公开。 必填项已用*标注