displaying multiple images serially within a loop

前端 未结 1 1518
抹茶落季
抹茶落季 2021-01-24 19:41

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

相关标签:
1条回答
  • 2021-01-24 20:14

    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).

    0 讨论(0)
提交回复
热议问题