0

我正在尝试将一些 UIImage 对象添加到数组中,但它不起作用。我尝试将图像添加到 NSArray 并且由于某种原因,该数组没有保存任何东西?

这是我的代码:

UIImage *a = [UIImage imageWithContentsOfFile:@"A.jpg"];
UIImage *b = [UIImage imageWithContentsOfFile:@"B.jpg"];
UIImage *c = [UIImage imageWithContentsOfFile:@"C.jpg"];
UIImage *d = [UIImage imageWithContentsOfFile:@"D.jpg"];
UIImage *e = [UIImage imageWithContentsOfFile:@"E.jpg"];
UIImage *f = [UIImage imageWithContentsOfFile:@"F.jpg"];

self.imageArray = [NSArray arrayWithObjects:a, b, c, d, e, f, nil];

int firstArrayCount = [self.array count];   
NSLog(@"%d objects in array 1", firstArrayCount);

int secondArrayCount = [self.imageArray count];
NSLog(@"%d objects in array 2", secondArrayCount);
4

2 回答 2

1

尝试使用UIImage加载图像。+imageNamed:例如,

UIImage *a = [UIImage imageNamed:@"A"];

检查所有这些图像是否已添加到您的项目中,而不仅仅是它们出现在 XCode 窗格中。如果做不到这一点,并且仅假设此代码不在同一范围内,请检查您的NSArray属性是否保留而不是分配。

于 2013-06-18T04:51:02.750 回答
1
UIImage *a = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"A" ofType:@".jpg"]];
UIImage *b = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"B" ofType:@".jpg"]];
UIImage *c = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"C" ofType:@".jpg"]];
UIImage *d = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"D" ofType:@".jpg"]];
UIImage *e = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"E" ofType:@".jpg"]];
UIImage *f = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"F" ofType:@".jpg"]];

NSArray *imageArray = [NSArray arrayWithObjects:a, b, c, d, e, f, nil];

int secondArrayCount = [imageArray count];
NSLog(@"%d objects in array 2", secondArrayCount);

或者

UIImage *a = [UIImage imageNamed:@"A.jpg"];
UIImage *b = [UIImage imageNamed:@"B.jpg"];
UIImage *c = [UIImage imageNamed:@"C.jpg"];
UIImage *d = [UIImage imageNamed:@"D.jpg"];
UIImage *e = [UIImage imageNamed:@"E.jpg"];
UIImage *f = [UIImage imageNamed:@"F.jpg"];

NSArray *imageArray = [NSArray arrayWithObjects:a, b, c, d, e, f, nil];

int secondArrayCount = [imageArray count];
NSLog(@"%d objects in array 2", secondArrayCount);
于 2013-06-18T04:54:12.243 回答