我有点困惑如何使用UIScrollview
或其他对象动态加载图像...
![动态显示来自webservice的图像] [1]
在上图中,它支持水平和垂直滚动图像。我们可以使用UIScrollview
或其他对象(例如UICollectionView
?
以下是用于显示7张图像的水平滚动视图的一般代码,
int x=10;
int y=20;
for (int i=1; i<=7; i++)
{
UIView *bgView=[[UIView alloc]initWithFrame:CGRectMake(x, y, 80, 80)];
bgView.tag=i;
UIImageView *bgImgView=[[UIImageView alloc]initWithFrame:CGRectMake(1, 1, 55, 55)];
bgImgView.tag=1;
bgImgView.image=[UIImage imageNamed:[NSString stringWithFormat:@"img_%d.png",i]];
[bgView addSubview:bgImgView];
[self.videoscrollview addSubview:bgView];
x=x+80;
}
self.scrollview.contentSize=CGSizeMake(7*80, 84);
我们可以使用UIView
处理一个UIImageView
和UIScrollview
方法,使用{{1}}来显示图片吗?
我想处理不同大小的UIViews,它们是UIScrollview ...
的子视图我用谷歌搜索但没有发现任何相关的东西。有什么建议可以解决这个问题吗?
答案 0 :(得分:1)
假设你的图像名称是喜欢:images_1 / 2 ... / 10 ... / n。(确保来自此图像名称的日期图像)
试试这个:
int x=10; int y=20;
for (int i=1; i<=7; i++)
{
UIView *bgView=[[UIView alloc]initWithFrame:CGRectMake(x, y, 80, 80)];
bgView.tag=i;
UIImageView *bgImgView=[[UIImageView alloc]initWithFrame:CGRectMake(1, 1, 55, 55)];
bgImgView.tag=1;
NSString *image = [NSString stringWithFormat:@"images_%d.png",i];
bgImgView.image = [UIImage imageNamed:image];
[bgView addSubview:bgImgView];
[self.videoscrollview addSubview:bgView];
x=x+80;
}
self.scrollview.contentSize=CGSizeMake(7*80, 84);
检查它是否正常工作,可能会起作用吗?
快乐的编码。
答案 1 :(得分:0)
为此,首先你必须计算并检查scrollview的可见区域,即contentOffset,然后你必须加载该可见部分的对象,当你分配新对象时,你应该将它们存储在一个数组中。当任何图像移动到可见区域之外时,您应该将其从滚动视图中移除。 这样它将占用更少的内存,您可以加载许多图像而不会收到内存警告。
您可以按照本文获取一些提示,以便如何在scrollView
中添加和删除对象http://www.raywenderlich.com/10518/how-to-use-uiscrollview-to-scroll-and-zoom-content
答案 2 :(得分:0)
Try this for dynamic image From NSBundle:
NSString * resourcePath = [[NSBundle mainBundle] resourcePath];
NSError * error;
NSArray * directoryContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:resourcePath error:&error];
NSMutableArray *imageArray = [[NSMutableArray alloc]init];
for (int i=0; i<directoryContents.count; i++) {
NSString *pathExtension = [[directoryContents objectAtIndex:i] pathExtension];
if ([pathExtension isEqualToString:@"png"] || [pathExtension isEqualToString:@"jpg"] ) {
[imageArray addObject:[directoryContents objectAtIndex:i]];
}
}
Now u have array of all images
for (int i=1; i<=imageArray.count; i++)
{
UIView *bgView=[[UIView alloc]initWithFrame:CGRectMake(x, y, 80, 80)];
bgView.tag=i;
UIImageView *bgImgView=[[UIImageView alloc]initWithFrame:CGRectMake(1, 1, 55, 55)];
bgImgView.tag=1;
bgImgView.image=[UIImage imageNamed:[imageArray objectAtIndex:i]];
[bgView addSubview:bgImgView];
[self.videoscrollview addSubview:bgView];
x=x+80;
} self.scrollview.contentSize = CGSizeMake(i * 80,84);
答案 3 :(得分:0)
感谢您的宝贵答案。我使用下面的代码暂时解决了上述问题。
Bool isOddViewNo; //在.h文件
isOddViewNo = YES; //在viewDidLoad
int x=10;
int y=10;
for (int i=0; i<=15; i++)
{
if (isOddViewNo)
{
isOddViewNo=NO;
UIView *oddView=[[UIView alloc]initWithFrame:CGRectMake(x, y, 90, 90)];
UIImageView *oddImgView=[[UIImageView alloc]initWithFrame:CGRectMake(1, 1, 85, 85)];
oddImgView.image=[UIImage imageNamed:[NSString stringWithFormat:@"img_%d.png",i]];
[oddView addSubview:oddImgView];
[self.scrollview addSubview:oddView];
x+=90;
}
else
{
isOddViewNo=YES;
UIView *evenView=[[UIView alloc]initWithFrame:CGRectMake(x, y, 150, 150)];
UIImageView *evenImgView=[[UIImageView alloc]initWithFrame:CGRectMake(1, 1, 145, 145)];
evenImgView.image=[UIImage imageNamed:[NSString stringWithFormat:@"img_%d.png",i]];
[evenView addSubview:evenImgView];
[self.scrollview addSubview:evenView];
x+=150;
}
if ((i+1)%3==0)
{
//if added image is 4 th image
y+=150;
x=10;
}
if (y+150>self.scrollview.frame.size.height)
{
self.scrollview.contentSize=CGSizeMake(400, y+150);
}
else
{
self.scrollview.contentSize=CGSizeMake(320, self.scrollview.frame.size.height+150);
}
}
![我在UIScrollview的每一行显示三个不同大小的视图。哪个可以水平和垂直滚动。这是从模拟器拍摄的屏幕截图] [1]