【问题标题】:Repeat image in UIScrollView in iPhone在 iPhone 的 UIScrollView 中重复图像
【发布时间】:2012-08-08 06:06:02
【问题描述】:

我是 iPhone 新手,

我想在 ScrollView 中重复我的 PatternImage。

逻辑:768bg.gif 是我的图像,我将此图像添加到UIImageView,然后将 Imageview 添加到UIScrollView,但图像没有重复。

这是我的代码 sn-p,

    CGRect scrollViewFrame = CGRectMake(0, 0, 1000, 600);
    UIScrollView *scrollVw = [[UIScrollView alloc] initWithFrame:scrollViewFrame];
    [self.view addSubview:scrollVw];
    [scrollVw release];

    UIImage *image1 = [UIImage imageNamed:@"768bg.gif"]; 
    self.imageView = [[UIImageView alloc] initWithImage:image1]; 
    [scrollVw addSubview:self.imageView]; 
    scrollVw.contentSize = CGSizeMake(image1.size.width+100, image1.size.height+100);

我们将不胜感激。

【问题讨论】:

  • 重复是什么意思??解释清楚..
  • 我想重复 PatternImage。
  • 是的,我尝试了这个UIImage *image1 = [UIImage imageNamed:@"768bg_with_footer.gif"]; self.imageView = [[UIImageView alloc] initWithImage:[self.view setBackgroundColor:[UIColor colorWithPatternImage:image1]]];,但显示将void 参数发送到UIImage 时出错
  • 检查我的答案 stackoverflow.com/questions/11758704/… 将 col 固定为 3 并将 row 固定为无限
  • [self.view] 不会给出图像,所以我认为你应该检查 Tejeshwar Gill 的答案。这是正确的。

标签: iphone ipad uiscrollview uiimageview uiimage


【解决方案1】:

您可以凭记忆使用[UIColor colorWithPatternImage:]

【讨论】:

  • 如何将colorWithPatternImage 添加到 ImageView 中?
  • [myView setBackgroundColor:[UIColor colorWithPatternImage:...]]
  • 我试过这个但得到错误UIImage *image1 = [UIImage imageNamed:@"768bg.gif"]; self.imageView = [[UIImageView alloc] initWithImage:[self.view setBackgroundColor:[UIColor colorWithPatternImage:image1]]];
  • 不,不。试试这个:ScrollView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"768bg.gif"]];.
  • 另外,尝试使用 PNG 而不是 GIF。 PNG 解码要快得多(它们在框架级别进行了优化),而 GIF 是调色板图像,需要在 32 位屏幕上显示颜色映射,这会导致性能下降。
【解决方案2】:

您可以参考此链接...。您要创建的是无限UIScrollview 对吗?? http://mobiledevelopertips.com/user-interface/creating-circular-and-infinite-uiscrollviews.html

这里有一些示例代码供您理解....

for(int i=0; i < [Array count]; i++)
{       
    UIImageView *img = [[UIImageView alloc] init];
    [img setUserInteractionEnabled:YES];
    [img setTag:i];
    UITapGestureRecognizer *ges = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(setImage:)];
    ges.numberOfTapsRequired = 1;
    [img addGestureRecognizer:ges];
    img.bounds = CGRectMake(10, 10, 50, 50);
    img.frame = CGRectMake(0, yOffset, 50, 50);
    img.image = [UIImage imageNamed:[Array objectAtIndex:i]];
    [images insertObject:img atIndex:i];    
    ScrollView.layer.cornerRadius = 11;
    [ScrollView addSubview:[images objectAtIndex:i]];            
    yOffset += 50;
} 
ScrollView.contentSize = CGSizeMake(58,yOffset);

【讨论】:

  • 是的,我想要无限滚动,但在垂直方向。
  • 这很简单,只需将 contentOffset 从 x 轴更改为 y 轴...。这意味着保持 y 轴作为变量,x 轴保持不变......检查我的编辑后的帖子添加了代码..
  • img 何时在您的代码中发布?多次重新加载相同的图像,并在视图中添加潜在的大量子视图已经是一个坏主意;但是在每次迭代时不释放它会导致这里出现巨大的内存泄漏。
【解决方案3】:
  1. 使用图形编辑器从您的 gif 中提取所有图像(iphone 无法“播放”gif)

  2. 保存生成的图像,例如 name1.png、name2.png、name3.png(或您需要的任何扩展名)

  3. 将图像导入您的项目,然后说:

UIImageView *myImageView = [UIImageView alloc] bla-bla-create-the-imageview];

    [myImageView setFrame:CGRectMake(bla-bla-whatever)];
            myImageView.animationImages = [NSArray arrayWithObjects:    
                                    [UIImage imageNamed:@"name1.png"],
                                    [UIImage imageNamed:@"name2.png"],
                                    [UIImage imageNamed:@"name3.png"],
                                    [UIImage imageNamed:@"name4.png"],
                                    nil];
    myImageView.animationDuration = 2.0;//or whatever time interval
    myImageView.animationRepeatCount = 0;
    [myImageView startAnimating];
    //add the imageVew to scroll

【讨论】:

  • @iPhone 开发者,他说他已经在子视图中添加了一个 gif 图像并且该图像没有被重复......谁会因为他无法提出正确的问题而感到内疚?到底是谁在 iphone 项目中添加了 gif 图像,而他只能看到顶层?
  • 好吧,我不会删除我的答案。它准确地反映了所提出的问题,我已经努力学习它,并努力发布它。
  • 看看标题:“在 iPhone 的 UIImageView 中重复图像”,这不是关于重复图像,也就是动画图像吗? ... 捂脸...
  • 我没有要求你这样做。但我认为让你知道问题的确切含义会很好。但是很好的努力!:)
  • 伙计们,您可以通过对我的答案投反对票将我卷入负无限,无论如何我都不会编辑或删除它。正如我所说,我已经回答了所提出的问题,当我这样做时,它是“在 iPhone 的 UIImageView 中重复图像”。有人说,愚蠢的问题会导致错误的答案......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多