我有一个在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);
输出:`第x帧:226.000000 y:226.000000 w:226.000000小时: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
我做错了什么?
答案 0 :(得分: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}}