I can 开发者_运维知识库not find out a easy way. Anyone help? Thanks!
Put your image name into array instead of images, that's much better memory-wise.
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
CGFloat currentOffset = 0;
for (int i = 0; i < [imageArray count]; i++) {
UIImageView *tmpImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[imageArray objectAtIndex:i]]];
currentOffset += tmpImageView.frame.size.width;
//currentOffset += tmpImageView.frame.size.height; // for vertical
tmpImageView.frame = CGRectOffset(tmpImageView.frame, currentOffset, 0);
//tmpImageView.frame = CGRectOffset(tmpImageView.frame, 0, currentOffset); // for vertical
[scrollView addSubview:tmpImageView];
[tmpImageView release];
}
scrollView.contentSize = CGSizeMake(currentOffset, scrollView.frame.size.height);
//scrollView.contentSize = CGSizeMake(scrollView.frame.size.width, currentOffset); // for vertical
Then add that scrollView to shichever view you want and release it.
As you can see this will put all your images one next to other horizontally. If you want to do it vertically, simply uncomment all the commented lines and delete the line before each of those commented lines.
This is how one would solve this problem with iOS4. iOS5 adds an easier way to do this but you'll need to look at the doc since it's still under NDA.
UIImageView *imageView = [[[UIImageView alloc] initWithFrame:CGRectZero] autorelease]; imageView.animationImages = [NSArray arrayWithObjects: [UIImage imageNamed:@"one.png"], [UIImage imageNamed:@"two.png"], [UIImage imageNamed:@"three.png"], [UIImage imageNamed:@"four.png"], nil];
精彩评论