我正在开发一个在Imageview上显示多个图像的视图,并且图像的计数是动态的。 我通过URL获取这些图像,URL的字符串存储在一个数组中。
我已经采用for循环为每个要在其上显示的图像创建新的imageview。基于图像的大小设置图像视图的帧。但问题在于,当图像数量超过5时,第六张图像会从屏幕中消失。但如果阵列中有超过5个图像,我希望它向下移动。
下面是我使用for-loop
在imageview上显示图像的代码int x=0;
for (int i=0;i<[items count]; i++)
{
NSLog(@"image name %@",[items objectAtIndex:i]);
NSLog(@"image url %@",[NSURL URLWithString:[NSString stringWithFormat:@"%@",[items objectAtIndex:i]]]);
NSString* urlTextEscaped = [[NSString stringWithFormat:@"%@",[items objectAtIndex:i]] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSData *imgData=[NSData dataWithContentsOfURL:[NSURL URLWithString:urlTextEscaped]];
UIImageView *imgV = [[UIImageView alloc]initWithImage:[UIImage imageWithData:imgData]];
imgV.frame=CGRectMake(x, 0, 50, 50);
[cell.contentView addSubview:imgV];
x=x+imgV.frame.size.width+10;
}
答案 0 :(得分:1)
转换为此图像
int x=0;
int y =0;
for (int i=0;i<[items count]; i++)
{
NSLog(@"image name %@",[items objectAtIndex:i]);
NSLog(@"image url %@",[NSURL URLWithString:[NSString stringWithFormat:@"%@",[items objectAtIndex:i]]]);
NSString* urlTextEscaped = [[NSString stringWithFormat:@"%@",[items objectAtIndex:i]] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSData *imgData=[NSData dataWithContentsOfURL:[NSURL URLWithString:urlTextEscaped]];
UIImageView *imgV = [[UIImageView alloc]initWithImage:[UIImage imageWithData:imgData]];
imgV.frame=CGRectMake(x, y, 50, 50);
[cell.contentView addSubview:imgV];
if(x>cell.frame.size.width)
{
x =0;
y= y+imgV.frame.size.height+10;
}
else
{
x=x+imgV.frame.size.width+10;
}
}
答案 1 :(得分:0)
您可以在滚动视图或桌面视图中显示图像,以便在有许多图像时无法在屏幕中显示所有这些图像。然后它将具有滚动功能。
答案 2 :(得分:0)
如果您使用的是故事板,请将 scrollview 添加到viewController的视图中,然后使用此示例代码可以帮助您。
#import "ViewController.h"
@interface ViewController ()
@property (strong, nonatomic) IBOutlet UIScrollView *scrollView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self setAutomaticallyAdjustsScrollViewInsets:YES];
[self addView:15];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void) addView:(NSInteger)numberOfViews
{
double x =10 , y=10;
for (NSInteger i=1; i <= numberOfViews; i++) {
UIView * view = [[UIView alloc] initWithFrame:CGRectMake(x, y, 50, 50)];
y = y+70;
[view setBackgroundColor:[UIColor purpleColor]];
[_scrollView addSubview:view];
}
}
@end
注意:在界面构建器中,单击您的滚动视图,然后单击属性检查器并选中(勾选)滚动视图部分
下的垂直弹跳选项