我的代碼需要更長的時間,然後預期。我怎樣才能減少加載時間?以前它不佔用這麼多時間。我沒有改變代碼,但它已經放慢了速度。 我的代碼如下流程:代碼需要更長的時間,然後預計
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];
}
一些380條記錄中productList的到來。我不認爲它應該花這麼多時間。
需要多少時間?你需要多少時間?你如何衡量需要多長時間? – 2011-05-19 13:48:02
它需要2到3秒。它應該在秒之內。 – DivineDesert 2011-05-19 13:51:27
您應該在單獨的線程上執行如此長的處理。 – 2011-05-19 14:09:16