Brodie
Brodie

Reputation: 3506

iphone threading question - looping?

i want to have a background thread in my app that changes an image every 5 seconds for as long as the app is being run. Can someone point me in the direction of how this works? I am new to threads.

Upvotes: 0

Views: 157

Answers (4)

user160917
user160917

Reputation: 9350

An NSTimer will probably do what you are looking to do, however, calls from NSTimer will normally block the main thread, if the process is involved, or you need to getting something from the internets to switch out the picture, you will need to create a new thread to do this.

For information on threading, I highly recommend the CS193P Lecture on performance, They go into detail on NSThread, NSOperations, ect.

Also, from Apple, Threading programing Guide.

Upvotes: 1

toto
toto

Reputation: 455

If you are using UIImageView and want an animated change between images you do not even need a timer. UIImageView can animate between images all by itself:

NSArray *images = [NSArray arrayWithObjects: [UIImage imageNamed: @"foo.png"],
                                             [UIImage imageNamed: @"bar.png"],
                                             nil];

yourImageView.animationImages = images;
yourImageView.animationDuration = 5.0s;
[yourImageView startAnimating];

The details are documented in the UIImageView docs.

Upvotes: 1

Claus Broch
Claus Broch

Reputation: 9212

You can use an NSTimer for this. No need to spawn off e new thread:

[NSTimer scheduledTimerWithTimeInterval:5.0s target:self selector:@selector(updateImage) userInfo:nil repeats:YES];

Upvotes: 0

Jaanus
Jaanus

Reputation: 17866

You do not need a thread for that. You can do it with a timer which is simpler than a thread. See the timer programming guide.

Upvotes: 0

Related Questions