2013-03-12 70 views
0

这是问题所在。 我想制作一个照片查看器应用程序,每个页面显示6个图像,并使用scrollview页面控件。在滚动视图中显示很多图像会导致延迟

但现在我有一个问题,因为添加到许多子视图导致滞后。我不知道如何处理。

这里是我的代码:

HUD = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES]; 
HUD.labelText = @"Loading..."; 
HUD.mode = MBProgressHUDAnimationFade; 
[self.view addSubview:HUD]; 
self.navigationItem.leftBarButtonItem.enabled = NO; 

const CGFloat itemWidth = 160.0f; //panjang 
const CGFloat itemHeight = 127.0f; 
const CGFloat buttonWidth = 157.0f; //panjang button 
const CGFloat buttonHeight = 125.0f; //tinggi button 
const CGFloat marginHorz = (itemWidth - buttonWidth)/3.0f; 
const CGFloat marginVert = (itemHeight - buttonHeight)/2.0f; 

NSURLRequest *request = [[NSURLRequest alloc] initWithURL:storyUrl]; 

AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) { 

    jsons = JSON; 
    NSString *urll; 

    int index = 0; 
    int row = 0; 
    int column = 0; 

    //start 
    for(id item in jsons){ 

     //[self.button removeFromSuperview]; 

     if ([item objectForKey:@"img_medium"] == [NSNull null]) { 

      urll=[item objectForKey:@"img_medium"]; 

     }else{ 

      urll=[item objectForKey:@"img2_medium"]; 

     } 


     button = [UIButton buttonWithType:UIButtonTypeCustom]; 

     UIImageView *imgggg = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, buttonWidth , buttonHeight)] ; 
     [imgggg setImageWithURL:[NSURL URLWithString:urll] placeholderImage:[UIImage imageNamed:@"no_photo.png"]]; 


     [button addSubview:imgggg]; 
     button.tag = index; 

     //button placing 
     button.frame = CGRectMake(column*itemWidth + marginHorz, row*itemHeight + marginVert, buttonWidth, buttonHeight); 
     [button addTarget:self 
        action:@selector(switchView:) 
     forControlEvents:UIControlEventTouchUpInside]; 

     NSLog(@"offset but %f",column*itemWidth + marginHorz); 
     NSLog(@"column %d",column); 
     NSLog(@"row %d",row); 
     //UIImage *pic = [UIImage imageNamed:@"1.jpg"]; 

     UILabel *labeling = [[UILabel alloc] initWithFrame:CGRectMake(0, 80, 157, 45)]; 
     [labeling setBackgroundColor:[UIColor colorWithWhite:0 alpha:0.45]]; 
     [labeling setText:[item objectForKey:@"title"]]; 
     [labeling setTextAlignment:NSTextAlignmentCenter]; 

     labeling.numberOfLines = 0; 
     [labeling setTextColor:[UIColor whiteColor]]; 
     [labeling setFont:[UIFont boldSystemFontOfSize:12.0]]; 

     //UIImageView *img = [[UIImageView alloc] initWithImage:pic]; 
     //[img setFrame:CGRectMake(0, 0, 50, 50)]; 
     [button addSubview:labeling]; 
     [scrollView addSubview:button]; 

     index++; 
     row++; 

     if (row == 3) { 
      row = 0; 
      column++; 
     } 

     NSLog(@"index %d",index); 

    } 
    //end 

    [MBProgressHUD hideHUDForView:self.view animated:YES]; 
    self.navigationItem.leftBarButtonItem.enabled = YES; 
    [self listSubviewsOfView:scrollView]; 

    //end block ui 
    //[[UIApplication sharedApplication] endIgnoringInteractionEvents]; 

} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) { 
    NSLog(@"Request Failed with Error: %@, %@", error, error.userInfo); 
    [MBProgressHUD hideHUDForView:self.view animated:YES]; 
    self.navigationItem.leftBarButtonItem.enabled = YES; 

    UIAlertView *errorAlert = [[UIAlertView alloc] initWithTitle:nil message:@"Request Time Out" delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil]; 
    [errorAlert show]; 
    //end block ui 

    //[[UIApplication sharedApplication] endIgnoringInteractionEvents]; 

}]; 
[operation start]; 

回答

0

前段时间我也面临着同样的问题,我已经使用PSTCollectionView。它具有非常强大的功能,如果您的应用程序在iOS6下运行,那么它将使用PSTCollectionView,如果您的应用程序在iOS6上运行,那么它将使用UICollectionView。

0

好像你需要实现视图回收。由于你正在实现一个scrollView,UITableView是UIScrollView的一个子类,它实现了很多单元。它通过重用单元来管理内存。

Here是您可能想要阅读的文档。

0

开关全部从它们的默认放大缩小你的滚动视图的子视图的CALayer的的插值方法linearnearestNeighbor,这将允许非常快速的升频和降频,在变焦完成块设置回无论他们之前(我建议三线顺畅的UI/gfx)

惊讶这不是UIScrollView默认的事情,考虑到苹果在所有的应用程序中都做这个方法来优化速度。