create multiple images view like the photo app iphone sdk
How can create a view with mutiple images, somethi开发者_StackOverflowng similiar to the photo app on the iphone?
THanks
So if you want a 4 by 4 grid of pictures (that are 20x20) each for example, you can use a scroll view and add UIImageViews to it something like
UIScrollView *s= [[UIScrollView alloc] initWithFrame:someFrame]
int position x=10;
int position y=10;
for(int i=1; i<=pictures.count; i++)
{
UIImageView *image= [[UIImageView alloc] initWithFrame:CGRectMake(x,y,20,20)]
image.image=[UIImage imageNamed:..] //initialize the image for the view
[s addSubview:image];
if(i%4==0) //means you need to start a new row
{
y+=20; //you can tweak this to make it look how you like
x=10; //come back to the beggining
}
else
x+=10; //again tweak this to make it "look" right
}
My standard answer: check out the TTPhotoViewController
in the Three20 project:
TTPhotoViewController emulates Apple's Photos app with all of its flick n' pinch delight. You can supply your own "photo sources", which works similiarly to the data sources used by UITableView. Unlike Apple's Photos app, it isn't limited to photos stored locally. Your photos can be loaded from the network, and long lists of photos can be loaded incrementally.
Works better for me (Daniel's code modified) :
UIScrollView *s = [[UIScrollView alloc] initWithFrame:someFrame];
int numOfColumns = 4;
int numOfRows = imagesArray.count/numOfColumns+1;
int space = 10;
int width = (s.frame.size.width-(numOfColumns+1)*space)/numOfColumns;
int height = width;
int x = space;
int y = space;
for (int i=1; i<=imagesArray.count; i++) {
UIImageView *image= [[UIImageView alloc] initWithFrame:CGRectMake(x,y,width,height)];
image.image = [UIImage imageWithData:[imagesArray objectAtIndex:i-1]];
[s addSubview:image];
[image release];
if (i%numOfColumns == 0) {
y += space+height;
x = space;
} else {
x+=space+width;
}
}
int contentWidth = numOfColumns*(space+width)+space;
int contentHeight = numOfRows*(space+height)+space;
[s setContentSize:CGSizeMake(contentWidth, contentHeight)];
[self.view addSubview:s];
imagesArray
is an Array
of NSData
精彩评论