Reputation: 1553
I m trying to create an image gallery where the images are stored in resource bundle. I'm storing my images in NSMutable array what i want is an image gallery ....but the output is pretty different from wat i expected.the below code works perfectly fine.to be more specific cud u guys help me out below is the code...
_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];
Upvotes: 0
Views: 963
Reputation: 1
This works very well but you would have to make a few adjustments to the code when you change the size. Copy and paste it in your Viewdidload
; make sure you have a UIScrollview
with IB already setup with a link name scrollView
.
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)];
Upvotes: 0
Reputation: 69027
The problem with your code is here:
if (coloumn==2) {
coloumn==0; //-- ERROR!
row++;
this should be:
coloumn=0; //-- correct!
using == instead of =
Upvotes: 2
Reputation: 31722
Use the three20 library
iPhone SDK: Creating a Photo Gallery With Three20
Edited:
Your code are excellent except on wrong statement :
coloumn==0;
that's the reason your colomn always point to the 2nd coloumn.
So change it to coloumn = 0;
Upvotes: 0