使用 NSArray 的 UIImage 对象显示一系列 UIImageView

发布于 2024-11-29 03:32:23 字数 26 浏览 0 评论 0原文

我找不到简单的方法。有人帮忙吗?谢谢!

I can not find out a easy way. Anyone help? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

枫林﹌晚霞¤ 2024-12-06 03:32:23

将图像名称放入数组而不是图像中,这在内存方面要好得多。

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

然后将该滚动视图添加到您想要的任何视图中并释放它。

正如您所看到的,这会将所有图像水平地并排放置。如果您想垂直执行此操作,只需取消注释所有注释行并删除每个注释行之前的行即可。

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.

后知后觉 2024-12-06 03:32:23

这就是使用 iOS4 解决这个问题的方法。 iOS5 添加了一种更简单的方法来执行此操作,但您需要查看文档,因为它仍处于 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];

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];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文