如何以编程方式显示图像水平 UIscrollView [英] how to display images horizontal UIscrollView Programmatically

查看:20
本文介绍了如何以编程方式显示图像水平 UIscrollView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我确实获取了 Url 的图像.然后在 SCrollView 上显示.我的代码是这样的

I did Get images for Url.and then display on SCrollView.My code like this

 NSMutableArray *al=[NSJSONSerialization JSONObjectWithData:webData options:0 error:nil];
    for (NSDictionary *diction in al)
    {


       NSString *geting =[diction valueForKey:@"dealimage"];
        NSLog(@"dealimage is %@",geting);

        NSData *getdata=[[NSData alloc]initWithData:[NSData dataFromBase64String:geting]];
        NSLog(@"good day %@",getdata);
        dataimages=[UIImage imageWithData:getdata];
        NSLog(@"dataimages %@",dataimages);

        [imagesarray addObject:dataimages];


     }

scrollView=[[UIScrollView alloc]init];

    scrollView.delegate = self;
    scrollView.scrollEnabled = YES;
    int scrollWidth = 100;
    scrollView.contentSize = CGSizeMake(scrollWidth,100);
    scrollView.frame=CGRectMake(0, 0, 320, 100);
    scrollView.backgroundColor=[UIColor yellowColor];

    [self.view addSubview:scrollView];

    int xOffset = 0;
    imageView.image = [UIImage imageNamed:[imagesName objectAtIndex:0]];

    for(int index=0; index < [imagesName count]; index++)
    {

        UIImageView *img = [[UIImageView alloc] init];
        img.bounds = CGRectMake(10, 10, 50, 50);
        img.frame = CGRectMake(5+xOffset, 0, 160, 110);
        NSLog(@"image: %@",[imagesName objectAtIndex:index]);
        img.image = [UIImage imageNamed:[imagesName objectAtIndex:index]];
        [images insertObject:img atIndex:index];
        scrollView.contentSize = CGSizeMake(scrollWidth+xOffset,110);

        [scrollView addSubview:[images objectAtIndex:index]];

        xOffset += 170;
    }

我的问题是 ScrollView 是显示但图像没有添加所以请告诉我我的代码有什么问题

my Problem is ScrollView is Display but Images are not add So please tell me what wrong in my code

现在我需要这样

我知道如何获取 Url 的图像.现在我需要显示图像 Horizo​​ntal ScrollView 所以请告诉我有关图像的任何想法.

I know how to get images for Url.now i need display images Horizontal ScrollView So Please give me any idea about images .

推荐答案

你的编码没问题,但是你分配了 [scrollView addSubview:[images objectAtIndex:index]]; 的值,这不是很好,我根据你的结果修改你的代码想要

your coding is Fine, but you are assign the value of [scrollView addSubview:[images objectAtIndex:index]];, this is not the fine one, I modify your code, as per your result Want

UIScrollView  *scrollVie=[[UIScrollView alloc]init];

scrollVie.delegate = self;
scrollVie.scrollEnabled = YES;
int scrollWidth = 100;
//scrollVie.contentSize = CGSizeMake(scrollWidth,100);
scrollVie.frame=CGRectMake(0, 51, self.view.frame.size.width, 100);
scrollVie.backgroundColor=[UIColor redColor];




int xOffset = 0;

for(int index=0; index < [imagesarray count]; index++)
{

    UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(xOffset,10,160, 110)];
    img.image = (UIImage*) [imagesarray objectAtIndex:index];

    [scrollVie addSubview:img];

    xOffset+=100;


}

[self.view addSubview:scrollVie];

scrollVie.contentSize = CGSizeMake(scrollWidth+xOffset,110);

这篇关于如何以编程方式显示图像水平 UIscrollView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆