我编写了从 JSON 请求中收集图像的代码,并尝试将它们添加到 NSMutableArray
以供稍后在 TableView 中使用。问题是在将图像对象添加到数组后,我的 NSMutableArray
的大小为 0。这是相关代码:
@implementation example{
NSMutableArray *_placeImages;
}
-(void)viewDidLoad{
...
_placeImages = [[NSMutableArray alloc] init];
}
-(void)JsonQuery {
...
// Retrieve the results of the URL.
dispatch_async(kMyQueue, ^{
[self downloadImages];
[self performSelectorOnMainThread:@selector(reloadTableData) withObject:NULL waitUntilDone:YES];
});
}
-(void)downloadImages{
...
NSData* data = [NSData dataWithContentsOfURL: url];
UIImage* image = [UIImage imageWithData:data];
[_placeImages addObject:image];
}
最佳答案
你必须首先分配 NSMutableArray:
_placeImages = [[NSMutableArray alloc] init];
或者你可以使用惰性初始化:
- (NSMutableArray *)placeImages
{
if (!_placeImages) {
_placeImages = [[NSMutableArray alloc] init];
}
return _placeImages
}
关于ios - 创建 UIImage 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25374522/