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 butto
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.
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++;
}
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).