0

我有点困惑如何完成使用UIScrollview或其他对象动态加载图像......

![动态显示来自网络服务的图像][1]

在上图中,它支持图像的水平和垂直滚动。我们可以使用UIScrollview或其他对象来做到这一点UICollectionView吗?

下面是用于显示 7 个图像的水平滚动视图的通用代码,

int x=10;
int y=20;

for (int i=1; i<=7; i++)
{
    UIView *bgView=[[UIView alloc]initWithFrame:CGRectMake(x, y, 80, 80)];
    bgView.tag=i;
    UIImageView *bgImgView=[[UIImageView alloc]initWithFrame:CGRectMake(1, 1, 55, 55)];
    bgImgView.tag=1;
    bgImgView.image=[UIImage imageNamed:[NSString stringWithFormat:@"img_%d.png",i]];
    [bgView addSubview:bgImgView];
    [self.videoscrollview addSubview:bgView];
    x=x+80;
}
self.scrollview.contentSize=CGSizeMake(7*80, 84);

我们可以使用一种类似上述方法来UIView显示图像吗?UIImageViewUIScrollview

我想处理不同大小的 UIView,它们是 UIScrollview 的子视图...

我用谷歌搜索但没有找到任何相关的东西。有什么建议可以解决这个问题吗?

4

4 回答 4

1

假设您的图像名称是 Likes:images_1/2.../10.../n。(确保来自此图像名称的动态图像)

试试这个:

int x=10; int y=20;

for (int i=1; i<=7; i++)
{
      UIView *bgView=[[UIView alloc]initWithFrame:CGRectMake(x, y, 80, 80)];
      bgView.tag=i;
      UIImageView *bgImgView=[[UIImageView alloc]initWithFrame:CGRectMake(1, 1, 55, 55)];
      bgImgView.tag=1;

      NSString *image = [NSString stringWithFormat:@"images_%d.png",i];
      bgImgView.image = [UIImage imageNamed:image];

     [bgView addSubview:bgImgView];
     [self.videoscrollview addSubview:bgView];
     x=x+80;
 }

 self.scrollview.contentSize=CGSizeMake(7*80, 84);

检查它是否工作,可能会工作?

快乐编码。

于 2013-10-22T12:05:48.097 回答
0

为此,首先您必须计算并检查滚动视图的可见区域,即 contentOffset ,然后您必须为该可见部分加载对象,并在分配新对象时将它们存储在数组中。当任何图像移出可见区域时,您应该将其从滚动视图中移除。这样它会占用更少的内存,并且您可以加载许多图像而不会收到内存警告。

您可以按照这篇文章来获得一些提示,如何在 scrollView 中添加和删除对象

http://www.raywenderlich.com/10518/how-to-use-uiscrollview-to-scroll-and-zoom-content

于 2013-10-22T12:11:23.333 回答
0
Try this for dynamic image From NSBundle:

NSString * resourcePath = [[NSBundle mainBundle] resourcePath];
NSError * error;
NSArray * directoryContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:resourcePath error:&error];
NSMutableArray *imageArray = [[NSMutableArray alloc]init];

for (int i=0; i<directoryContents.count; i++) {
    NSString *pathExtension = [[directoryContents objectAtIndex:i] pathExtension];
    if ([pathExtension isEqualToString:@"png"]  || [pathExtension isEqualToString:@"jpg"] ) {
        [imageArray addObject:[directoryContents objectAtIndex:i]];
    }
}
Now u have array of all images

for (int i=1; i<=imageArray.count; i++)
{
  UIView *bgView=[[UIView alloc]initWithFrame:CGRectMake(x, y, 80, 80)];
  bgView.tag=i;
  UIImageView *bgImgView=[[UIImageView alloc]initWithFrame:CGRectMake(1, 1, 55, 55)];
  bgImgView.tag=1;
  bgImgView.image=[UIImage imageNamed:[imageArray objectAtIndex:i]];
  [bgView addSubview:bgImgView];
 [self.videoscrollview addSubview:bgView];
  x=x+80;

} self.scrollview.contentSize=CGSizeMake(i*80, 84);

于 2013-10-22T12:38:02.003 回答
0

感谢您的宝贵回答。我使用以下代码暂时修复了上述问题。

Bool isOddViewNo; //在.h文件

isOddViewNo=YES; //在 viewDidLoad

int x=10;
int y=10;

for (int i=0; i<=15; i++)
{
    if (isOddViewNo)
    {
        isOddViewNo=NO;
        UIView *oddView=[[UIView alloc]initWithFrame:CGRectMake(x, y, 90, 90)];
        UIImageView *oddImgView=[[UIImageView alloc]initWithFrame:CGRectMake(1, 1, 85, 85)];
        oddImgView.image=[UIImage imageNamed:[NSString stringWithFormat:@"img_%d.png",i]];
        [oddView addSubview:oddImgView];
        [self.scrollview addSubview:oddView];
        x+=90;
    }
    else
    {
        isOddViewNo=YES;
        UIView *evenView=[[UIView alloc]initWithFrame:CGRectMake(x, y, 150, 150)];
        UIImageView *evenImgView=[[UIImageView alloc]initWithFrame:CGRectMake(1, 1, 145, 145)];
        evenImgView.image=[UIImage imageNamed:[NSString stringWithFormat:@"img_%d.png",i]];
        [evenView addSubview:evenImgView];
        [self.scrollview addSubview:evenView];
        x+=150;
    }

    if ((i+1)%3==0)
    {
        //if added image is 4 th image
        y+=150;
        x=10;
    }

    if (y+150>self.scrollview.frame.size.height)
    {
        self.scrollview.contentSize=CGSizeMake(400, y+150);
    }
    else
    {
        self.scrollview.contentSize=CGSizeMake(320, self.scrollview.frame.size.height+150);
    }
}

![我在 UIScrollview 的每一行显示三个不同大小的视图。它可以水平和垂直滚动。这是从模拟器中截取的屏幕截图][1]

于 2013-10-24T05:58:22.880 回答