在我的應用程序中,內存問題有點問題。
我的編碼技能還不夠完善。
我的代碼使用內存四次,我真的需要。
我該如何更改我的編碼?如何將圖像名稱添加到數組中?
我的代碼是
-(void)viewDidAppear:(BOOL)animated
{
UIScrollView * ScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 44, self.view.frame.size.width, self.view.frame.size.height-88)];// 44(UpperNavigtionBar),-88(Upper+Lower)
ScrollView.pagingEnabled = YES;
// Create a UIImage to hold Info.png
UIImage *image1 = [UIImage imageNamed:@"Image-001.jpg"];
UIImage *image2 = [UIImage imageNamed:@"Image-002.jpg"];
UIImage *image3 = [UIImage imageNamed:@"Image-003.jpg"];
UIImage *image4 = [UIImage imageNamed:@"Image-004.jpg"];
UIImage *image5 = [UIImage imageNamed:@"Image-005.jpg"];
UIImage *image6 = [UIImage imageNamed:@"Image-006.jpg"];
UIImage *image7 = [UIImage imageNamed:@"Image-007.jpg"];
UIImage *image8 = [UIImage imageNamed:@"Image-008.jpg"];
UIImage *image9 = [UIImage imageNamed:@"Image-009.jpg"];
UIImage *image10 = [UIImage imageNamed:@"Image-010.jpg"];
UIImage *image11 = [UIImage imageNamed:@"Image-011.jpg"];
UIImage *image12 = [UIImage imageNamed:@"Image-012.jpg"];
UIImage *image13 = [UIImage imageNamed:@"Image-013.jpg"];
NSArray *images = [[NSArray alloc] initWithObjects:image1,image2,image3,image4,image5,image6,image7,image8,image9,image10,image11,image12,image13,nil];
NSInteger numberOfViews = 13;
for (int i = 0; i < numberOfViews; i++)
{
CGFloat xOrigin = i * self.view.frame.size.width;
UIImageView * ImageView = [[UIImageView alloc] initWithFrame:CGRectMake(xOrigin, 0, self.view.frame.size.width, self.view.frame.size.height-88)]; // -88(Upper+Lower)
[ImageView setImage:[images objectAtIndex:i]];
[ScrollView addSubview:ImageView];
}
ScrollView.contentSize = CGSizeMake(self.view.frame.size.width*numberOfViews, self.view.frame.size.height-88); // -88(for adding Image View as subview)
[self.view addSubview:ScrollView];
}
您是如何確定您的代碼正好使用它所需內存四倍的? – 2013-02-17 15:01:11
@ H2CO3,首先,我製作了13個圖像對象。其次,將這些對象添加到數組中。第三,將圖像添加到圖像視圖中。第四,將圖像視圖添加到滾動視圖。那是我認爲的H2CO3。我如何更改我的代碼? – 2013-02-17 15:05:15
對不起,如果這聽起來很刺耳,但我認爲你應該'梳理'你的基本知識。這些對象通常是通過引用傳遞的,只有通過值傳遞給它的指針,所以你的編號根本沒有意義。 (這適用於很多語言) – 2013-02-17 15:07:25