添加UIImageView到NSArray崩溃



我想知道如何将UIImageView添加到NSArray中而不会崩溃。

当前我正在做这个。

//.h
@property (strong, nonatomic) UIImageView *transButtonImageView;
@property (strong, nonatomic) UIImage *transButtonImage;
//.m
@synthesize transButtonImageView;
@synthesize transButtonImage;
transButtonImage = [UIImage imageNamed:@"trans.png"];
[transButtonImageView setImage:transButtonImage];
 NSMutableArray *arrayOfButtonImages = [[NSMutableArray alloc] init];
[arrayOfButtonImages addObject:transButtonImageView]; // this is where the error happens

当我尝试将transButtonImageView添加到NSArray时如果我查看imageView的值它设置为nil,即使我添加了图像?

没有崩溃日志,所以错误可能来自任何地方,但以下是可能的:

  1. UIImageView初始化了吗?我们知道,nil等于NSInteger 0,不是一个对象,所以当nil被添加到任何容器中时,都会发生崩溃;

  2. 容器是否初始化了容量?我们只能在可变容器中添加或删除对象,它必须是一个有容量的NSMutableArray

  3. 可能图片太大,占用内存太多,导致内存警告。

以上你最好都试试。好运!

你需要使用NSMutableArray来修改它。然后你可以使用[mutableArray copy]将它复制到NSArray。

最新更新