2013-03-12 66 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默認的事情,考慮到蘋果在所有的應用程序中都做這個方法來優化速度。