创建图像库

时间:2011-06-03 07:02:00

标签: objective-c xcode

我正在尝试创建一个图像库,其中图像存储在资源包中。我将我的图像存储在NSMutable数组中我想要的是一个图像库....但是输出与我预期的相当不同。下面的代码工作得很好。更具体的cud你们帮助我下面是代码......

enter image description here

_images =[NSMutableArray arrayWithObject:[UIImage imageNamed:@"logo1.png"]];
[_images  addObject:[UIImage imageNamed:@"logo2.png"]];
[_images  addObject:[UIImage imageNamed:@"logo3.png"]];
[_images  addObject:[UIImage imageNamed:@"logo4.png"]];
[_images  addObject:[UIImage imageNamed:@"logo5.png"]];
[_images  addObject:[UIImage imageNamed:@"logo6.png"]];
[_images  addObject:[UIImage imageNamed:@"logo7.png"]];
[_images  addObject:[UIImage imageNamed:@"logo8.png"]];
[_images  addObject:[UIImage imageNamed:@"logo9.png"]];
[_images  addObject:[UIImage imageNamed:@"logo10.png"]];
[_images  addObject:[UIImage imageNamed:@"logo11.png"]];
[_images  addObject:[UIImage imageNamed:@"logo12.png"]];
[_images  addObject:[UIImage imageNamed:@"logo13.png"]];
[_images  addObject:[UIImage imageNamed:@"logo14.png"]];
[_images  addObject:[UIImage imageNamed:@"logo15.png"]];
[_images  addObject:[UIImage imageNamed:@"logo16.png"]];

NSLog(@"ha ha ha:%d",_images.count);



UIScrollView *view = [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
int row=0;
int coloumn=0;
for (int i=0; i< _images.count; i++)
{
    UIImage *thumb=[_images objectAtIndex:i];
    UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
    button.frame=CGRectMake(coloumn*100+24, row*80+30, 64, 64);
    [button setImage:thumb forState:UIControlStateNormal];
    [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    button.tag=i;
    [view addSubview:button];
    if (coloumn==2) {
        coloumn==0;
        row++;
    }else {
        coloumn++;
    }

}

[view setContentSize:CGSizeMake(320, (row+1)*80+10)];
self.view=view;
[view release];

3 个答案:

答案 0 :(得分:2)

您的代码存在问题:

if (coloumn==2) {
    coloumn==0;   //-- ERROR!
    row++;

这应该是:

    coloumn=0;   //-- correct!

使用==而不是=

答案 1 :(得分:0)

使用three20库

iPhone SDK: Creating a Photo Gallery With Three20

enter image description here

<强>编辑:

您的代码非常出色,除非声明错误:

coloumn==0;

这就是你的colomn始终指向第二个coloumn的原因。

因此将其更改为 coloumn = 0;

答案 2 :(得分:0)

这非常有效,但是当您更改大小时,您必须对代码进行一些调整。将其复制并粘贴到Viewdidload;确保您已使用链接名UIScrollview设置了scrollView的IB。

int row    = 0;
int colomn = 0;
int width  = 79;
int height = 79;
int offset = 1;
// int diff   = width * 0.5;
CGRect rect;

for (int i = 0; i < 45; i++) {

    if(i == 0){

        colomn = 0;

    }else{

        colomn += width + offset;

    };

    if(colomn >= ((int)scrollView.frame.size.width) - width){

        colomn = 0;
        row += height + offset;

    }

       rect =  CGRectMake(colomn, row, width, height);


    UIView *view = [[UIView alloc] initWithFrame:rect];
    [view setBackgroundColor:[UIColor redColor]];
    [scrollView addSubview:view];

}

[scrollView setContentSize:CGSizeMake(320, row+height)];