在循环内连续显示多个图像 [英] displaying multiple images serially within a loop

查看:88
本文介绍了在循环内连续显示多个图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在循环中获得流式显示。我的理解是,原则上我可以通过放置一个与按钮推动相关联的无限循环来挂起程序。在任何情况下,当我运行此代码时,运行计数器打印出来就好了,并且捕获了来自摄像机的帧(我可以将它们保存为一系列文件。)但是,使用setImage的显示无需做任何事情直到结束循环时,它显示屏幕上的最后一个图像。我的问题是:
如果程序按照文档中的描述顺序运行,为什么不显示显示,而display命令另一端的代码处理得很好?显示器是否被简单地转移到一个并行进程中,在这种情况下我应该放一个等待显示完成的延迟循环?

I am trying to get a streaming display within a loop. My understanding is that I could, in principle, hang the program by putting an infinite loop associated with, say, a button push. In any case, when I run this code, the running counter prints out just fine, and the frames from the camera are captured (I can save them as a series of files.) However, the display using setImage does nothing until the end of the loop, when it displays the last image on the screen. My question is this: If the program runs serially as described in the documentation, why does the display not occur, while the code on other side of the display command processes just fine? Is the display simply "dumped off" into a parallel process, in which case I should just put a delay loop waiting for the display to complete?

谢谢
WMW

Thanks WMW

uint32 MaxRuns = 10;
for (uint32 NoRuns = 0 ; NoRuns < MaxRuns ; NoRuns++)   
{
//
//Here is some code to capture each image from the camera
//and save them to image.tif
//
inFilePath = @"/Volumes/Portable_Mac/Programming/CameraData/image.tif";
    [TestImage initWithContentsOfFile:inFilePath];      
[viewWindow setImage: TestImage];
    printf("run number:%d\n",NoRuns);
}


推荐答案

图纸未倾销关闭另一个线程,但它被推迟。当你调用 setImage:时,视图的图像 ivar会被更改,很可能setter也会调用 setNeedsDisplay: 。这就是视图需要重绘的信号。因为绘图是一件昂贵的事情,所以它只是每次运行完成一次(具体来说,在结束时)运行循环,直到你的才会发生for 循环结束。

The drawing isn't dumped off to another thread, but it is deferred. When you call setImage:, the view's image ivar gets changed, and most likely the setter also calls setNeedsDisplay:. This is how the view signals that it needs to be redrawn. Because drawing is an expensive thing to do, however, it only gets done once per pass (specifically, at the end) of the run loop, which doesn't happen until your for loop ends.

显示一系列图像的最佳方式几乎肯定是重复计时器( performSelector:withObject:afterDelay: 也值得一看)。将所有图像加载到一个数组中,然后启动计时器:

Tthe best way to do show a series of images would almost certainly be a repeating timer (performSelector:withObject:afterDelay: is also worth looking at). Load all your images into an array, then start the timer:

[NSTimer scheduledTimerWithTimeInterval:2.5
                                 target:self
                               selector:@selector(changeDisplayedImage:)
                               userInfo:nil
                                repeats:YES];

然后你的计时器动作将改变视图的图像:

Then your timer's action will change the view's image:

- (void)changeDisplayedImage:(NSTimer *)tim {
    // currImageIdx is an ivar keeping track of our position
    if( currImageIdx >= [imageArray count] ){
        [tim invalidate];
        return;
    }
    [viewWindow setImage:[imageArray objectAtIndex:currImageIdx]];
    currImageIdx++;
}

请注意,除了实际绘图的延迟,设置内容视图可能会发生得太快,你不会看到图像。如果确实希望您的视图立即绘制,则发送 display 应该有效(如 setNeedsDisplay中所述) : description)。

Note that except for the delay from the actual drawing, setting the contents of the view would probably happen so fast you wouldn't see the image. If you really really want your view to draw immediately, however, sending it display should work (as mentioned in the setNeedsDisplay: description).

这篇关于在循环内连续显示多个图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆