【问题标题】:Display a sequence of images like a video?像视频一样显示一系列图像?
【发布时间】:2015-12-14 13:55:37
【问题描述】:

我正在 iOS 上试验图像处理算法。我有一个包含图像 test_frame1 ... test_frame100 (等)的资产目录,我在上面进行了一些处理。

除此之外,我还需要按顺序显示图像,就好像它们是视频的帧一样。

我首先想到的是在延迟几毫秒后依次更改UIImage 的图像。我正在异步执行此操作。代码如下:

    let imageShowQueue = dispatch_queue_create("com.grigoryptashko.ImageShowQueue",
        DISPATCH_QUEUE_SERIAL)
    let frameShowDelay = Int64(0.2 * Double(NSEC_PER_SEC)) // show images with 200 ms delay, but it doesn't actually work this way
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0)) {
        var dTime = dispatch_time(DISPATCH_TIME_NOW, frameShowDelay)
        for i in 1...1000 {
            dispatch_after(dTime, self.imageShowQueue) {
                dispatch_async(dispatch_get_main_queue()) {
                    NSLog("test_frame\((i % 100) + 1)")
                    self.imgView.image = UIImage(named: "test_frame\((i % 100) + 1)")
                    self.imgView.setNeedsDisplay()
                }
            }

            dTime = dispatch_time(dTime, frameShowDelay)
        }
    }

它确实有效,但 FPS 很快变得太低,例如 1 FPS 甚至更慢。有没有其他方法可以做到这一点?

也许,有一些我不知道的方法。例如,最近我了解了Accelerate 框架和Apple Metal。蚂蚁其他想法?

【问题讨论】:

    标签: ios animation image-processing uiimageview uiimage


    【解决方案1】:

    听起来你想要一个基于图像的动画。下面是你要做的:

    在你的viewDidLoad

    self.<name of the UIImageView you want to animate>.animationImages = [[NSArray alloc] initWithObjects: [UIImage imageNamed:@"Image Name"],
                                         [UIImage imageNamed:@"Image Name"],
                                         [UIImage imageNamed:@"Image Name"],
                                         [UIImage imageNamed:@"Image Name"],
                                         [UIImage imageNamed:@"Image Name"],
                                         [UIImage imageNamed:@"Image Name"],
                                         [UIImage imageNamed:@"Image Name"],
                                         [UIImage imageNamed:@"Image Name"],nil];
                          /* Do this until you have all the images in the array */
    self.<name of the UIImageView you want to animate>.animationDuration = 1.0f;
    self.<name of the UIImageView you want to animate>.animationRepeatCount = 1;
    

    然后您使用[&lt;name of the UIImageView you want to animate&gt; startAnimating]; 来启动动画。 animationDurationanimationRepeatCount 的数字可以更改为您希望它们的值。 Here 是苹果文档的链接。

    对于 Swift 开发:

    <name of the UIImageView you want to animate>.animationImages = [
                                             UIImage(named: "Image Name")!,
                                             UIImage(named: "Image Name")!,
                                             UIImage(named: "Image Name")!,
                                             UIImage(named: "Image Name")!,
                                             UIImage(named: "Image Name")!,
                                             UIImage(named: "Image Name")!,
                                             UIImage(named: "Image Name")!,
                                             UIImage(named: "Image Name")!
                                           ]
                              /* Do this until you have all the images in the array */
    <name of the UIImageView you want to animate>.animationDuration = 1.0
    <name of the UIImageView you want to animate>.animationRepeatCount = 1
    

    【讨论】:

      【解决方案2】:

      通过在UIImage 上使用animatedImageNamed: 可以更直接地执行此操作。看看这个:

      UIImage *animatingImage = [UIImage animatedImageNamed:@"test_frame_" duration:1.0];
      UIImageView *anImageView = [[UIImageView alloc] initWithImage:animatingImage];
      

      【讨论】:

        【解决方案3】:

        试试这个,改变数组中的图像并编译项目,在这部分设置时间间隔

        animationImageView.animationDuration = 0.5;
        

        http://www.appcoda.com/ios-programming-animation-uiimageview/

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-12-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-04-10
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多