timer in secondary thread is too slow

Discussion in 'iOS Development' started by acca2o, Feb 10, 2010.

  1. acca2o

    acca2o New Member

    Joined:
    Sep 26, 2009
    Messages:
    51
    Likes Received:
    0
    Device:
    2G iPod touch
    My goal is to show 50 images in 2 seconds (a kind of movie).
    I'm trying in this way, by a secondary thread and a NSTimer.
    But it seems the ipod is too slow and nothing changes if the timer delay is 0.001 or 0.01
    Where is the problem? Is there a solution?


    [OBJC]
    #import "ViewController.h"


    @implementation ViewController
    -(void)loadView
    {
    //root view
    imageView = [[UIImageView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    [self.view addSubview:imageView];
    [imageView release];

    //imagesLoader thread
    [NSThread detachNewThreadSelector

    Please Register or Log in to view images

    selector(imagesLoader) toTarget:self withObject:nil];
    }


    -(void)imagesLoader
    {
    NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
    NSTimer* timer = [NSTimer scheduledTimerWithTimeInterval:0.04 target:self selector

    Please Register or Log in to view images

    selector(showNextImage) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
    [[NSRunLoop currentRunLoop] run];
    [pool release];
    }


    -(void)showNextImage
    {
    imageIndex++;
    if(imageIndex > 50)
    imageIndex = 1;

    NSString* imageName = [NSString stringWithFormat

    Please Register or Log in to view images

    "%@%d%@", @"resources/images/palla", imageIndex, @".jpg"];
    //newImage = [[UIImage imageNamed:imageName] retain];
    [imageView.image release];
    imageView.image = [UIImage imageNamed:imageName];
    //[self performSelectorOnMainThread

    Please Register or Log in to view images

    selector(showImage) withObject:nil waitUntilDone:NO];
    }


    -(void)dealloc
    {
    [imageView.image release];
    [self.view release];
    [super dealloc];
    }
    @end[/OBJC]
  2. Axis

    Axis Super Moderator Staff Member

    Joined:
    Dec 2, 2007
    Messages:
    6,288
    Likes Received:
    133
    Device:
    iPhone 4S (White)
    1.) You can't expect NSTimer to be that accurate.

    2.) Reading a new image into memory every split second is not the best idea, especially for what you are trying to do.

    UIImageView has a property called 'animationImages' see the docs for more info. It shouldn't be too hard to figure out.
  3. NolesFans

    NolesFans New Member

    Joined:
    Sep 23, 2008
    Messages:
    404
    Likes Received:
    0
    Device:
    iPod touch
    NEVER run any UI object in the secondary threads.
  4. acca2o

    acca2o New Member

    Joined:
    Sep 26, 2009
    Messages:
    51
    Likes Received:
    0
    Device:
    2G iPod touch
    UIImageView animationImages property seems to be a good idea to overcame the NSTimer limitation, and maybe to increase the efficency.

    What about if my next step is to receive the images in realtime from a network connection?
    It means that i need to change the images continuosly.
  5. lauNchD

    lauNchD Well-Known Member

    Joined:
    Jan 27, 2008
    Messages:
    1,844
    Likes Received:
    261
    Device:
    iPhone 5 (Black)
    Oh, that project.
    Just use NS[Mutable]URL(Request|Connection) asynchronously and trigger a reload every time an image has downloaded.
    ------------------double post merged------------------
    BTW: I think it's nearly impossible to get 25fps by downloading images from a server. You should try video streaming or something…

Share This Page