ios - 如何以编程方式显示图像水平 UIscrollView

标签 ios objective-c uiscrollview uiimageview

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

 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 是显示但图像没有添加所以请告诉我我的代码有什么问题

现在我需要这样的image2

我知道如何为 Url 获取图像。现在我需要显示图像 Horizo​​ntal ScrollView 所以请给我任何关于图像的想法。

最佳答案

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

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);

关于ios - 如何以编程方式显示图像水平 UIscrollView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24837202/

相关文章:

ios - 在不覆盖自定义更改的情况下更新 GitHub iOS SDK

ios - 在 iPhone 上查找用户的 Documents 目录的最佳方法是什么?

ios - 计算字符串中数组中的对象数量

ios - 从 Parent ViewController 访问包含的 UIView 组件

iphone - Target Membership 变灰(禁用)

ios - 左侧的 UITableView 滚动指示器

ios - 统一 ARKit : How can I get trackingState properly?

objective-c - 如何比较两个 NSImages 的差异?

ios - 在 iOS 中滚动可变高度的组件

ios - scrollView 在 View 之间滑动