0

我有一个从 JSON 创建的 NSDictionary:

bookDictionary = [NSJSONSerialization JSONObjectWithData: bookData
                 options: NSJSONReadingMutableContainers
                 error: &anError];

每本书都有很多资产,每个资产都有一层。最多有 3 层。资产可以具有文本或图像类型。

我正在努力完成两件事。首先创建 3 个新字典,layer1、layer2 和 layer3,并用每一层的资产填充它们。

对于我要下载并保存图像以供以后使用的每个图像资产,这是我用来保存图像的代码:

NSString *stringURL = [NSString stringWithFormat:@"URL/%@",[assetData objectForKey:@"content" ]];
NSURL  *url = [NSURL URLWithString:stringURL];
NSData *urlData = [NSData dataWithContentsOfURL:url];

NSArray   *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString  *documentsDirectory = [paths objectAtIndex:0];

NSString  *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory,@"filename.png"];
[urlData writeToFile:filePath atomically:YES];

对于在本地保存后的每个图像资产,我需要用 filePath 替换 url。

建议会很有帮助,如果有人有与 NSDictionaries 类似的操作的示例代码,那就太好了

JSON 请求中的示例数据:

"average rating":"4",
"# of ratings":"41",
"list price":"43.99",
"your price":"43.15",
"thumburl":"/img/avatar.png",
"assets":[
            {
        "id":"8",
        "type":"Text",
        "content":"T-Bar%20Controller",
        "layer":"layer-1",
        "top":"190",
        "left":"74",
        "width":"130",
        "height":"28",
        "action":"",
        "url":""
     },
             {
        "id":"9",
        "type":"Text",
        "content":"Scroller",
        "layer":"layer-1",
        "top":"304",
        "left":"804",
        "width":"118",
        "height":"27",
        "action":"",
        "url":""
     },
            "id":"63",
        "type":"Image",
        "content":"ugc/ronny/25/assets/pho.JPG",
        "layer":"layer-2",
        "top":"0",
        "left":"0",
        "width":"340",
        "height":"191",
        "action":"",
        "url":""}
4

0 回答 0