0

我有一组在 CGRect 框架内创建的图像

.h 文件:

NSMutableArray *imageArray;

.m 文件:

 myImage = [[UIImageView alloc] initWithFrame:myImageRect];
[imageArray addObject:myImage];

如果我直接从 myImage 变量调用框架,我会得到正确的值:

NSLog(@"Frame x:%f y:%f w:%f h:%f", myImage.frame.origin.x, myImage.frame.origin.y, myImage.frame.size.width, myImage.frame.size.height);

输出:`frame x:226.000000 y:226.000000 w:226.000000 h:334.000000`

但是,如果我通过数组调用图像,它会给我不正确的值:

UIImageView *anImage = [imageArray objectAtIndex:5];        
NSLog(@"frame x:%f y:%f w:%f h:%f", anImage.frame.origin.x, anImage.frame.origin.y, anImage.frame.size.width, anImage.frame.size.height);

输出:frame x:0.000000 y:0.000000 w:0.000000 h:0.000000

我究竟做错了什么?

4

1 回答 1

0

你可能错的是数组的初始化

NSMutableArray *imageArray=[[NSMutableArray alloc]initWithCapacity:5];

试过了

NSMutableArray *imageArray=[[NSMutableArray alloc]initWithCapacity:5];
UIImageView *  myImage ;
CGRect myImageRect=CGRectMake(10, 10, 10, 10);

for (int i=0; i<5; i++) {
    myImage= [[UIImageView alloc] initWithFrame:myImageRect];
    [imageArray addObject:myImage];

}
UIImageView *anImage = [imageArray objectAtIndex:3];
NSLog(@"%@",NSStringFromCGRect(anImage.frame));

结果

2013-04-03 01:10:49.114 TrialsError[95088:11303] {{10, 10}, {10, 10}}
于 2013-04-02T19:42:40.387 回答