【问题标题】:How to use lazy loading in UIButton image?如何在 UIButton 图像中使用延迟加载?
【发布时间】:2011-09-17 06:53:26
【问题描述】:

我使用这个逻辑在滚动视图中创建按钮。 `int m=0; int j = [图像数据计数]/3; int s = [图像数据计数]%3; 如果(s==1 || s==2) { j=j+1; } scrollView.contentSize = CGSizeMake(320,j*155);

    int i,k,x=0,y=0;
    for(i=0;i<j;i++)
    {
        if(s==0){
            for(k=0;k<3;k++)
            {
                button = [UIButton buttonWithType:UIButtonTypeCustom];          
                [button setFrame:CGRectMake(13+x,10+y, 91,135)];
                int p;
                NSLog(@"%i",p++);
                [button setTag:m];
                [button setBackgroundColor:[UIColor whiteColor]];
                NSData *imagesubCategoryData = [[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:[imageData objectAtIndex:m]]];
                [button setImage:[UIImage imageWithData:imagesubCategoryData] forState:UIControlStateNormal];
                [button addTarget:self action:@selector(buttonPress:) forControlEvents:UIControlEventTouchUpInside];
                [scrollView addSubview:button];
                x=x+101;
                m++;
            }
        }
        else if(s==1)
        {
            for(k=0;k<3;k++)
            {
                button = [UIButton buttonWithType:UIButtonTypeCustom];

                [button setFrame:CGRectMake(13+x,10+y, 91,135)];
                [button setTitle:@"Button"  forState:UIControlStateNormal];
                int p;
                NSLog(@"%i",p++);
                [button setTag:m];
                [button setBackgroundColor:[UIColor whiteColor]];
                NSData *imagesubCategoryData = [[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:[imageData objectAtIndex:m]]];
                [button setImage:[UIImage imageWithData:imagesubCategoryData] forState:UIControlStateNormal];
                [button addTarget:self action:@selector(buttonPress:) forControlEvents:UIControlEventTouchUpInside];
                [scrollView addSubview:button];
                x=x+101;
                if(i==j-1 && k==0)
                {
                    break;
                }
                m++;
            }
        }
        else if(s==2)
        {
            for(k=0;k<3;k++)
            {

                button = [UIButton buttonWithType:UIButtonTypeCustom];;
                [button setFrame:CGRectMake(13+x,10+y, 91,135)];
                [button setTitle:@"Button"  forState:UIControlStateNormal];
                int p;
                NSLog(@"%i",p++);
                [button setTag:m];
                [button setBackgroundColor:[UIColor whiteColor]];
                NSData *imagesubCategoryData = [[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:[imageData objectAtIndex:m]]];
                [button setImage:[UIImage imageWithData:imagesubCategoryData] forState:UIControlStateNormal];
                [button addTarget:self action:@selector(buttonPress:) forControlEvents:UIControlEventTouchUpInside];
                [scrollView addSubview:button];
                x=x+101;
                if(i==j-1 && k==1)
                {
                    break;
                }
                m++;
            }
        }

        x=0;
        y=y+145;
    }

`

【问题讨论】:

  • 试着解释一下你想达到什么,“延迟加载按钮图像”不是很清楚。
  • 在这种方法中,我创建了一个按钮并在按钮上设置图像。所有按钮创建和图像设置然后滚动视图显示我想在按钮上一一加载图像的数据
  • 您好,我想在滚动视图中创建许多按钮,然后在该按钮上加载图像。

标签: iphone uiscrollview uibutton


【解决方案1】:

你可以在here找到延迟加载的演示代码

希望从这个你可以得到延迟加载的逻辑......

【讨论】:

    猜你喜欢
    • 2020-08-12
    • 2023-03-07
    • 2021-04-02
    • 2012-09-15
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    • 2014-08-27
    • 1970-01-01
    相关资源
    最近更新 更多