Iphone 代码需要比预期更长的时间
我的代码比预期的时间长。如何缩短装载时间?以前没有花这么多时间。我没有更改代码,但它的速度变慢了。 我的代码流程如下:Iphone 代码需要比预期更长的时间,iphone,objective-c,xcode,cocos2d-iphone,ios,Iphone,Objective C,Xcode,Cocos2d Iphone,Ios,我的代码比预期的时间长。如何缩短装载时间?以前没有花这么多时间。我没有更改代码,但它的速度变慢了。 我的代码流程如下: for (i =1 ; i< [productList count]; i++) { UIImage *image; products *productItem = [productList objectAtIndex:i-1]; if(![productItem.productItemPhoto isEqualToString:@""]){
for (i =1 ; i< [productList count]; i++) {
UIImage *image;
products *productItem = [productList objectAtIndex:i-1];
if(![productItem.productItemPhoto isEqualToString:@""]){
NSString *productItemPhoto = productItem.productItemPhoto;
NSData* data = [NSData dataWithContentsOfFile:productItemPhoto];
image = [[UIImage alloc] initWithData:data];
}
else{
if(numberOfProductsPerRow == 1)
image = [UIImage imageNamed:@"no-image-2.png"];
else
image = [UIImage imageNamed:@"no-image-1.png"];
}
UIImageView *bg1;
if(numberOfProductsPerRow == 1)
bg1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image-box-s7.png"]];
else
bg1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image-box-s4.png"]];
bg1.frame = CGRectMake(x, y,width, height);
[productScrollView addSubview:bg1];
UIButton *pro1 = [[UIButton alloc] initWithFrame:CGRectMake(x+spacingX, y+spacingY-15, btnWidth, btnHeight)];
[pro1 setImage:image forState:UIControlStateNormal];
[pro1 setTag:i];
[pro1 addTarget:self action:@selector(selectProduct:) forControlEvents:UIControlEventTouchUpInside];
[productScrollView addSubview:pro1 ];
UILabel *lblProductModel = [[UILabel alloc] initWithFrame:CGRectMake(0, height - 40, width, 30)];
lblProductModel.backgroundColor = [UIColor clearColor];
lblProductModel.textAlignment = UITextAlignmentCenter;
lblProductModel.textColor = [UIColor colorWithRed:1.0 green:0.8 blue:0.0 alpha:1];
NSString *price;
if([userSettings.priceToShow isEqualToString:@"WholesalePrice"])
price = [NSString stringWithFormat:@"%@%.0f",productItem.productCurrencySymbol, productItem.productWholesalePrice];
else if([userSettings.priceToShow isEqualToString:@"RetailsalePrice"])
price = [NSString stringWithFormat:@"%@%.0f",productItem.productCurrencySymbol, productItem.productRetailSalesValue];
else
price = @"";
lblProductModel.tag = [productList count] + i;
lblProductModel.text = [NSString stringWithFormat:@"%@ %@", productItem.productModelCode, price];
[bg1 addSubview:lblProductModel];
x = x + width + 10;
if(i%numberOfProductsPerRow == 0){
x = 20;
y=y+height+10;
}
[pro1 release];
[image release];
[bg1 release];
[lblProductModel release];
}
if((i-1)%numberOfProductsPerRow!=0)
scrollViewParent.contentSize = CGSizeMake(0, y+height+spacingY);
else
scrollViewParent.contentSize = CGSizeMake(0, y + spacingY);
productScrollView.contentSize = scrollViewParent.contentSize;
[scrollViewParent addSubview:productScrollView];
}
for(i=1;i<[productList count];i++){
UIImage*图像;
products*productItem=[productList objectAtIndex:i-1];
如果(![productItem.productItemPhoto IsequalString:@“”){
NSString*productItemPhoto=productItem.productItemPhoto;
NSData*data=[NSData data WITH CONTENTS OFFILE:productItemPhoto];
image=[[UIImage alloc]initWithData:data];
}
否则{
if(numberOfProductsError==1)
image=[UIImage ImageName:@“no-image-2.png”];
其他的
image=[UIImage ImageName:@“no-image-1.png”];
}
UIImageView*bg1;
if(numberOfProductsError==1)
bg1=[[UIImageView alloc]initWithImage:[UIImageName:@“image-box-s7.png”];
其他的
bg1=[[UIImageView alloc]initWithImage:[UIImageName:@“image-box-s4.png”];
bg1.frame=CGRectMake(x,y,宽度,高度);
[productScrollView添加子视图:bg1];
UIButton*pro1=[[UIButton alloc]initWithFrame:CGRectMake(x+spacingX,y+spacingY-15,btnWidth,btnHeight)];
[pro1 setImage:image for状态:UIControlStateNormal];
[pro1 setTag:i];
[pro1 addTarget:self action:@selector(selectProduct:)for ControlEvents:UIControlEventTouchUpInside];
[productScrollView添加子视图:pro1];
UILabel*lblProductModel=[[UILabel alloc]initWithFrame:CGRectMake(0,高度-40,宽度,30)];
lblProductModel.backgroundColor=[UIColor clearColor];
lblProductModel.textAlignment=UITextAlignmentCenter;
lblProductModel.textColor=[uicolorWithred:1.0绿色:0.8蓝色:0.0 alpha:1];
NSString*价格;
如果([userSettings.priceToShow IsequalString:@“批发商价格”])
价格=[NSString stringWithFormat:@“%@%.0f”,productItem.productCurrencySymbol,productItem.Product批发商价格];
else if([userSettings.priceToShow IsequalString:@“RetailsalePrice”])
价格=[NSString stringWithFormat:@“%@%.0f”,productItem.productCurrencySymbol,productItem.productRetailSalesValue];
其他的
价格=@;
lblProductModel.tag=[productList count]+i;
lblProductModel.text=[NSString stringWithFormat:@“%@%@”,productItem.productModelCode,price];
[bg1 addSubview:lblProductModel];
x=x+宽度+10;
如果(i%NumberOfProductsError==0){
x=20;
y=y+高度+10;
}
[pro1释放];
[图片发布];
[bg1发布];
[LBLPProductModel发布];
}
如果((i-1)%NumberOfProductsError!=0)
scrollViewParent.contentSize=CGSizeMake(0,y+高度+间距);
其他的
scrollViewParent.contentSize=CGSizeMake(0,y+spacingY);
productScrollView.contentSize=scrollViewParent.contentSize;
[scrollViewParent addSubview:productScrollView];
}
大约有380张唱片被列入产品目录。我认为这不应该花这么多时间。考虑到在该代码中使用
UI*
API的工作量,该代码必须在主线程上运行。您正在主线程上加载大量图像,相对而言,图像加载是一个非常缓慢和昂贵的操作
首先,您真的需要在开始时加载所有380个图像吗?当增长到500、1500或15000时会发生什么?我敢打赌你的应用程序在380到15000之间的某个地方内存不足
您的代码应该只加载它需要的图像,然后在循环之外加载。在主线程上运行的任何东西都会在其运行期间阻止用户交互;保持这些时间段尽可能短(或者,理想情况下,完全不通过将计算移动到后台队列/线程)是理想的。需要多少时间?你预计要花多少时间?你是如何测量它需要多长时间的?需要2到3秒。它应该在几分之一秒之内。你应该在一个单独的线程上进行如此长的处理。如果你没有更改代码,那么它必须是已更改的测试数据,例如较大的图像等。如果所有380条记录都包含图像,然后2-3秒真的没有那么长。我在第一个屏幕上加载了图像,即一次加载8个图像,当用户向下滚动时,剩余的图像被加载。现在代码运行良好。太棒了;这听起来也是一个更具可扩展性的设计!在某个时候,您可能需要考虑卸载以前加载的图像,以确保不会耗尽内存(如果图像数量增加)。此外,我正在实现分页,即每次100个图像。图像的大小导致内存泄漏。所以找到了这样的解决方案…:)