如何在uiscrollview中水平加载多个uiimage?

时间:2013-06-27 07:00:07

标签: ios uiscrollview uiimageview uiimage

我制作了一个UIScrollView并显示了Array中的图像,但图像没有显示

这是我的代码

- (void)viewDidLoad
{
    [super viewDidLoad];

imageScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(13.0, 50.0, 292.0, 69.0)];

    [self makeScroll];

}

-(void)makeScroll
{
    imageScroll.delegate = self;

    imageScroll.backgroundColor = [UIColor clearColor];

    int arrayCount = [myArray count];

    imageScroll.contentSize = CGSizeMake(arrayCount * 101.0, 69.0);

    for (int i = 0; i < arrayCount; i++)

    {

        participantView = [[UIView alloc]init];

        if (i == 0) {

            participantView.frame = CGRectMake(0.0, 2.0, 91.0, 67.0);

        }

        else {

            participantView.frame = CGRectMake(101.0*i, 2.0, 91.0, 67.0);

        }

        participantView.backgroundColor = [UIColor yellowColor];

        //declaring the view in which image object will be added
        UIImageView *bannerImage = [[UIImageView alloc] 

initWithFrame:CGRectMake(0.0,0.0,91.0,67.0)];

        //taking out the image object
        UIImage *imageNamed = [UIImage imageNamed:[myArray objectAtIndex:0]];

        //resizing the image

   // UIImage *imageNamed2 = [imageNamed 

imageByScalingAndCroppingForSize:CGSizeMake(91.0,67.0)];

        //setting the resized image in UIImageView

        [bannerImage setImage:imageNamed];

        //clear the bg
        bannerImage.backgroundColor = [UIColor clearColor];

        bannerImage.autoresizingMask = UIViewAutoresizingFlexibleHeight;

        bannerImage.contentMode = UIViewContentModeScaleAspectFit;

        [bannerImage setNeedsLayout];

        bannerImage.frame = CGRectMake(0.0,0.0,91.0,67.0);

    [participantView addSubview:bannerImage];



            [imageScroll addSubview:participantView];

    }

    [self.view addSubview:imageScroll];


}

任何人都可以告诉我我在哪里做错了

问题是:图像未显示在scrollview

7 个答案:

答案 0 :(得分:2)

图片未显示,因为您添加了图片作为UIView的子视图。

记住层次结构。

首先将图像添加为scrollview的子视图,然后将scrollview添加为UIView的子视图。

或者,如果您使用的是故事板,则可以直接从那里设置层次结构。

答案 1 :(得分:0)

   scrl_venuelist.delegate=self;
    scrl_venuelist.contentSize =CGSizeMake(273 * [imageArr count], 137);

    int scroll_x=0;
    for (int i=0; i < [imageArr count]; i++)
    {
        UIImageView *img = [[[UIImageView alloc] initWithFrame:CGRectMake(scroll_x, 0, 100, 100)] autorelease];
        img.image =[UIImage imageNamed:[[imageArr objectAtIndex:i] objectForKey:@"ImgName"]];
        [scrl_venuelist addSubview:img];
        scroll_x = scroll_x + 273;
    }
}

答案 2 :(得分:0)

请参阅以下代码。这对我有用:

            scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0,50, 768, 40)];
            int scrollWidth=60;
            for (int i=0;i<array.count;i++)
            {
                UIImageView *imageView1=[[UIImageView alloc]initWithFrame:CGRectMake(scrollWidth,8,50,40)];
                imageView1.image=[array objectAtIndex:i];
                [scrollView addSubview:imageView1];
                scrollWidth=scrollWidth+80;
            }
            [scrollView setContentSize:CGSizeMake(scrollWidth, 30)];
            [self.view addSubview:scrollView];

答案 3 :(得分:0)

试试这个 -

设置页面数

    static NSUInteger numberOfPages = [imageArr count];
ViewDidLoad中的

    howItWorksScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 100, 320, 640)];
    howItWorksScrollView.pagingEnabled = YES;
    howItWorksScrollView.contentSize = CGSizeMake(howItWorksScrollView.frame.size.width * numberOfPages, howItWorksScrollView.frame.size.height);
    howItWorksScrollView.showsHorizontalScrollIndicator = YES;
    howItWorksScrollView.showsVerticalScrollIndicator = NO;
    howItWorksScrollView.scrollsToTop = NO;
    howItWorksScrollView.delegate = self;
    [self.view addSubview:howItWorksScrollView];

    int k,i;
    int btnX = 70, btnY = 25;

    int count =0;


    for (k = 0; k < numberOfPages; k++)
    {
        for (i = 0; i < 1; i++)
        {
            if (count < [imageArray count])
            {
                UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(btnX, btnY, 180, 180)];
                imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@.png",[imageArray objectAtIndex:k]]];
                imageView.backgroundColor = [UIColor clearColor];
                [howItWorksScrollView addSubview:imageView];
                [imageView release];

                count++;

                btnX = btnX + 180 + 70 + 70;

            }

            btnX = btnX - 320;

        }
        btnX = btnX + 320;
        btnY = 25;

    }

答案 4 :(得分:0)

看看VSScrollview。它是一个非常简单易用的类。它的使用方式与使用UITableview的方式相同,并且像UITableview重用其单元格一样,VSScrollView重用其视图,因此当Scrollview上有大量子视图时,您不必担心处理内存。 VSScrollview link

答案 5 :(得分:0)

Nicklockwood的

SwipeView是一个很好的库,可以在水平滚动视图中处理视图。

来自文档:

  

SwipeView是一个旨在简化实现的类   iOS上的水平,分页滚动视图。它基于a   UIScrollView,但添加了方便的功能,如   用于加载视图的UITableView样式的dataSource / delegate接口   动态,高效地查看装载,卸载和回收。

     

SwipeView的界面和实现基于iCarousel   图书馆,对于使用过iCarousel的人来说应该很熟悉。

答案 6 :(得分:-2)

感谢朋友们帮助我

我从错误的地方得到了问题,

在我的阵列中,图像是URL,所以我做了这个及其工作

UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0f, 91.0f, 67.0f)];

NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString: [myArray objectAtIndex:i]]];

UIImage *img = [[UIImage alloc] initWithData:data];

[imageView setImage:img];

问题解决了: - )