【问题标题】:Showing a UIImage after each iteration每次迭代后显示 UIImage
【发布时间】:2013-02-07 10:26:03
【问题描述】:

我有一个功能可以更改需要很长时间才能完成的图像(大约 10 秒)。这个图像每次迭代都会改变,我想每次都显示它。

我愿意

self.imageView.image = [ /* function that changes image */ ];

但我接受为什么那行不通。

我还尝试了以下方法:

 self.imageView.animationImages = [NSArray arrayWithObjects:[self.brain newImage],
                                                            [self.brain newImage],
                                                             nil];

但动画开始需要很长时间。

如何制作每次调用函数时更新的动画?

注意:[self.brain newImage] 是返回 UIImage 的有效函数,并且每次都会更改。我没有包含实际代码,因为它很复杂而且不是问题。

【问题讨论】:

    标签: iphone animation uiimageview uiimage jquery-animate


    【解决方案1】:

    您可以尝试使用此代码在图像数组上设置动画

    if(imageArray.count>0)
    {
        self.imageView.animationImages= imageArray;
        self.imageView.animationDuration = 1/20;
        self.imageView.animationRepeatCount =1;
        [self.imageView startAnimating];
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:1.0];
        [UIView commitAnimations];
    }
    

    我已将动画时间设置为 1/20,您可以设置自己的 FPS,并且动画重复计数为 0 以永久重复

    【讨论】:

      【解决方案2】:

      如果animationImages 方法适合您,您可以通过提前创建图像来加快启动速度:

      //-- preload images (e.g., while the app is loading)
      NSArray* images = [NSArray arrayWithObjects:[self.brain newImage],
                                                              [self.brain newImage],
                                                               nil];
      //-- display animation
      self.imageView.animationImages = images;
      

      请记住,animationImages 方法有局限性,主要是因为您需要提前创建所有图像 - 这可能会占用大量内存。

      另一种方法是使用CALayerNSTimer:每次触发时间时,您将layer.contents 设置为下一个图像。有关此方法的更多信息,请参阅this

      对于更高级的方法,您可以求助于 cocos2d 或 openGL,但这可能有点矫枉过正。

      【讨论】:

      • 这是我试图避免的。在加载应用程序时,图像的生成需要很长时间。我会研究你的第二种方法。
      • 是的,animationImages 非常易于使用,但它不能很好地控制您可以做什么。 CALayer+timer 方法肯定更灵活,也没有那么复杂。
      【解决方案3】:

      考虑在-newImage 方法中添加一个块参数:

      - (void)newImageWithSuccessBlock:(void (^)(UIImage *newImage))successBlock
      {
          // generate the image on a background thread
          UIImage *brainImage = ...
      
          // if successful and the completion block isn't nil, run it
          if (successBlock) {
              // NB: if you do use a background queue,
              // remember to dispatch the completion block to the main queue (not shown)
              successBlock(brainImage)  
          }
      
      }
      

      然后,在从视图控制器生成新图像时,您可以更新图像视图:

      [self.brain newImageWithSuccessBlock:^(UIImage *newImage) {
      
          NSMutableArray *mutableImageArray;
          if (self.imageView.animationImages) {
              mutableImageArray = [self.imageView.animationImages mutableCopy];
          } else {
              mutableImageArray = [NSMutableArray array];
          }
      
          [mutableImageArray addObject:newImage];
      
          self.imageView.animationImages = [NSArray arrayWithArray:mutableImageArray];
      
      }];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-04-11
        • 1970-01-01
        • 2018-04-12
        • 2023-04-03
        • 2018-09-01
        • 1970-01-01
        相关资源
        最近更新 更多