【问题标题】:UIImageView in NSArrayNSArray 中的 UIImageView
【发布时间】:2014-04-30 14:19:14
【问题描述】:

我必须在NSMutableArray 中缓存一些UIImageView,但是当我进入这个数组时,这个数组总是空的。

我使用这个代码:

NSMutableArray *currentImages = [[NSMutableArray alloc] init];
UIImageView *imageV = [[UIImageView alloc]initWithFrame:CGRectMake(20, 8, 50, 50)];
for (int i = 0; i< 20; i++) {
     imageV.image = [UIImage imageNamed:@"defaut.png"];
     [currentImages addObject:imageV];
}

你能帮帮我吗?

【问题讨论】:

  • 您需要显示更多代码。你在哪里存储currentImages?为什么要将图像视图存储在数组中?这是你的真实代码吗?

标签: objective-c uiimageview nsarray


【解决方案1】:

你可能想尝试这样的事情:

NSMutableArray *currentImages = [[NSMutableArray alloc] init];

for (int i = 0; i< 20; i++) {
     UIImageView *imageV = [[UIImageView alloc]initWithFrame:CGRectMake(20, 8, 50, 50)];
     imageV.image = [UIImage imageNamed:@"defaut.png"];
     [currentImages addObject:imageV];
}

不确定您是否需要这个,因为它可能会降低您的性能。我通常将 UIImage 名称存储为 NSStringsNSArray 中,然后对它们做任何我想做的事情。

【讨论】:

    【解决方案2】:

    您总是添加相同的图像视图,其中您更改了图像,您应该在循环中重新创建图像视图。

    根据您要实现的目标,存储图像而不是图像视图可能会更好,并使用图像更新您的一个图像视图。 缓存视图很少是您想要的。

    【讨论】:

    • 那么 OP 将有 20 个对同一个图像视图的引用,这肯定不是他想要的,但与他声称的“空”不同。
    • 我必须插入图像视图,因为我必须缓存它们。这段代码只是一个例子
    • @Anna565 您必须显示访问数组的位置,因为没有足够的上下文来确定您看不到内容的原因
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-25
    相关资源
    最近更新 更多