【问题标题】:How to add an images in Scrollview in iOS?如何在 iOS 的 Scrollview 中添加图像?
【发布时间】:2015-01-07 19:25:36
【问题描述】:

我是 iOS 开发的新手,我想在编写代码时向 UIScrollView 添加一个图像数组,然后它只显示数组的最后一个图像到 UIScrollview。我不明白是什么问题,请给我解决方案

我为此写了一个代码

 for(int index=0; index < [self.imagesa count]; index++)
{
    NSDictionary *dict=[self.imagesa objectAtIndex:index];
    NSString *image=[dict valueForKey:@"link"];
    self.zoomImage.bounds=CGRectMake(0, 0, self.zoomScroll.frame.size.width +10.0f, self.zoomScroll.frame.size.height);
    self.zoomImage.frame=CGRectMake(index * self.zoomScroll.frame.size.width, 0, self.zoomScroll.frame.size.width, self.zoomScroll.frame.size.height);
    [self.zoomImage setUserInteractionEnabled:YES];
    [self.zoomImage setMultipleTouchEnabled:YES];
    [self.zoomImage sd_setImageWithURL:[NSURL URLWithString:image] placeholderImage:[UIImage imageNamed:@"1.png"]];
    [self.zoomImage setUserInteractionEnabled:TRUE];
    [self.objectarray insertObject:self.zoomImage atIndex:index];
    CGSize scrollViewSize=CGSizeMake(self.zoomScroll.frame.size.width*[self.objectarray count], self.zoomScroll.frame.size.height);
    [self.zoomScroll setContentSize:scrollViewSize];
    [self.zoomScroll addSubview:self.zoomImage];
    [self.zoomScroll addSubview:[self.objectarray objectAtIndex:index]];
}

当我创建一个本地图像视图时,它显示的代码是this link shows my old code 它正在工作,但是当我为此编写代码时,它只显示最后一个数组图像,请给我解决方案。我知道这个问题问了很多次,但我没有得到解决方案。

【问题讨论】:

  • zoomImage 是你的 ScrollView?如果是,那么 index 的值是多少?
  • 兄弟我编辑我的代码,这里 zoomImage 是我的 ImageView 和 ZoomScroll 是我的 Scrollview 我想将 zoomimage 添加到 ZoomScroll 中。
  • 您使用“TableView”而不是使用滚动视图,并通过声明图像数组将图像添加到每个单元格。你正在以困难的方式做这件事,结果对两者来说都是一样的。
  • 你在哪里分配zoomImage(uiimageView)?
  • 我在我的 .h 文件中分配 zoomimage,例如 @property(retain,nonatomic)IBOutlet UIImageView *zoomImage;并且 Scrollview 也像 @property(retain,nonatomic)IBOutlet UIScrollView *zoomScroll;

标签: ios iphone uiscrollview


【解决方案1】:

您应该创建新的 imageView。 for(int index=0; index < [self.imagesa count]; index++) {UIImageView *imageView = [[UIImageView alloc] init……]}而不是总是使用self.zoomImage

【讨论】:

  • #mengxiangjian 它正在工作,但是我想在按下缩放按钮时缩放该图像视图,然后它的大小没有扩展,所以如果您有任何扩展大小的解决方案,请在我的 .h 文件中制作图像视图我的图像视图。
【解决方案2】:

你可以试试这个:

    for(int index=0; index < [self.imagesa count]; index++)
    {
        NSDictionary *dict=[self.imagesa objectAtIndex:index];
        NSString *image=[dict valueForKey:@"link"];
        UIImageView img=[[UIImageView alloc] init]; // allocate new object
        img.bounds=CGRectMake(0, 0, self.zoomScroll.frame.size.width +10.0f, img.frame.size.height);
        img.frame=CGRectMake(index * self.zoomScroll.frame.size.width, 0, self.zoomScroll.frame.size.width, self.zoomScroll.frame.size.height);
        [img setUserInteractionEnabled:YES];
        [img setMultipleTouchEnabled:YES];
        [img sd_setImageWithURL:[NSURL URLWithString:image] placeholderImage:[UIImage imageNamed:@"1.png"]];
        [img setUserInteractionEnabled:TRUE];
        [self.objectarray insertObject:img atIndex:index];
        [self.zoomScroll addSubview:img];
    }
    CGSize scrollViewSize=CGSizeMake(self.zoomScroll.frame.size.width*[self.objectarray count], self.zoomScroll.frame.size.height);
    [self.zoomScroll setContentSize:scrollViewSize];

【讨论】:

  • #Rijo Joseph 我知道它正在工作,但我想在按下缩放按钮时扩展我的图像视图大小然后我如何扩展我的图像视图的大小?我扩展了滚动视图大小,但图像视图大小未扩展。
  • 通过扩展大小你到底想做什么?您想单独缩放此滚动视图中的图像吗?
  • #Rijo Joseph 我想在按下缩放按钮时单独缩放带有图像的 ScrollView 我为此编写代码,例如 self.zoomScroll.frame=CGRectMake(10, 40, 300, 400);然后滚动视图大小被扩展但图像视图保持不变。当我使用你的代码时。
  • 当你点击缩放时,它是否会打开一个带有此图像的新视图,以便用户可以捏缩放?
  • #Rijo Joseph 不,我不想打开一个新视图,因为我的应用程序已经包含许多 xib,我希望它们进入一个视图以显示缩放结果。按下缩放时我没有打开我显示我当前视图中的缩放滚动视图。
猜你喜欢
  • 2015-01-11
  • 1970-01-01
  • 1970-01-01
  • 2012-09-10
  • 2023-03-21
  • 2017-05-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多