我确实为 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 是显示但图像没有添加所以请告诉我我的代码有什么问题
现在我需要这样的
我知道如何为 Url 获取图像。现在我需要显示图像 Horizontal 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/