0

这就是问题所在。我想制作一个照片查看器应用程序,它为每个页面显示 6 张图像,并使用滚动视图页面控件。

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

这是我的代码:

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];
4

3 回答 3

0

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

于 2013-03-12T07:22:44.173 回答
0

似乎您需要实现视图回收。由于您正在实现滚动视图,因此 UITableView 是 UIScrollView 的子类,它实现了很多单元格。它通过重用单元来管理内存。

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

于 2015-02-20T06:38:33.943 回答
0

将scrollView的所有子视图的CALayer的放大和缩小插值方法从默认值linear切换nearestNeighbor为/gfx)

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

于 2016-09-01T03:24:39.497 回答