我在JSON中获取图片网址列表。现在我需要逐个解析图像URL并将其转换为EGOImage
并将其添加到数组中以便使用该图像执行动画。现在我得到的错误是图像是nil
并且无法添加到数组中。
注意:图片网址没有问题。
我的JSON结构是
{
"images":[
{
"url":"https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcRQmkaIO86rYB-DctBruyv4lFTEM-v_pGG9k5i0lrAr2Elzibvuiw"
},
{
"url":"https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcQGh1nRGgnbUZXgKLMKGdeT320PGttnLmbN4R-DxmckQpjVDhHS"
}
]
}
我的代码:
NSMutableArray*arrForImageURL = [dic objectForKey:@"images"];
arrForUniqueWorderImages=[[NSMutableArray alloc]init];
for (NSDictionary*dictForImg in arrForImageURL)
{
NSString*stringForImageURL=[dictForImg objectForKey:@"url"];
imgView.imageURL=[NSURL URLWithString:stringForImageURL];
[arrForUniqueWorderImages addObject:(EGOImageView*)imgView.image];
}
答案 0 :(得分:0)
您正在尝试将EGOImageviews存储在数组中,为此您必须先正确初始化它。试试这个
NSMutableArray*arrForImageURL = [dic objectForKey:@"images"];
arrForUniqueWorderImages=[[NSMutableArray alloc]init];
for (NSDictionary*dictForImg in arrForImageURL)
{
EGOImageView *imageView = [[EGOImageView alloc] init];
imageView.contentMode=UIViewContentModeScaleAspectFit;
NSString*urlString=[dictForImg objectForKey:@"url"];
NSURL *imageUrl=[NSURL URLWithString:urlString];
imageView.imageURL =imageUrl;
imageView.frame=CGRectMake(0,0,320,300);
[arrForUniqueWorderImages addObject:imageView];
}