ios - 创建 UIImage 数组

标签 ios objective-c uiimage nsmutablearray

我编写了从 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/

相关文章:

ios - 设备人脸识别 - Swift

ios - Firebase-崩溃报告的后备

ios - MagicalRecord 几个截断, "then cannot find data for a temporary oid"

ios - 为什么我们不应该直接使用默认的QoS?

objective-c - 通用应用程序的命名约定

iphone - NSString 超出范围问题?

javascript - 用于搜索产品的 Ebay.com 查询字符串是什么?

uiimage - 从附加到AVPlayer的CALayer获取UIImage(从视频播放中提取帧)

swift - 裁剪(至 : CGRect) doesn't crop correctly

swift - 如何访问函数变量并将其从 super View 中删除?