在scrollview中显示大量图像会导致滞后

时间:2013-03-12 07:17:40

标签: ios image-processing uiscrollview uiimageview lag

这是问题所在。 我想制作一个照片查看器应用程序,每个页面显示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];

3 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

将所有scrollView的子视图的CALayer插值方法从默认的linear切换到nearestNeighbor进行放大和缩小,这将允许非常快速的放大和缩小,在缩放完成块中将其设置回以前的任何东西(我建议使用三线片来获得最流畅的UI / gfx)

惊讶这不是UIScrollView的默认设置,因为Apple在所有应用程序中都采用了这种方法来进行速度优化。