problem updating uiimageview from NSTimer method - iphone

I have problems updating the image of an UIImageView from within a method that is called from a NSTimer.
It works within the viewDidLoad method where I do something like this
[imageView setImage:[self getNextImage]];
later on I do
NSTimer* readTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(readSource:) userInfo:nil repeats:NO];
[readTimer fire];
that works and the readSource method gets called.
There I put this code:
UIImageView* tempImageView = (UIImageView*)[containerView viewWithTag:nextDefragSourcePosition ];
That returns exactly the imageView I want to have -
but the following line does not change the image of the UIImageView
[tempImageView setImage:dataBeingReadImage];
Any ideas ? Thanks in advance.
Heiko

You are aware that you are updating the GUI from a different thread than where the UIImageView is created? You can execute selectors on the main thread to work around this problem.
Just take a look at the performSelectorOnMainThread method. Just call that from the method that gets called when your NSTimer triggers (readSource:) and supply it with the image you want to display. Then just update the UIImageView from THAT selector (not the readSource one) and it SHOULD work.
There really isn't that much code for me to go on.

Related

How to make a history for a web browser

I'm making my own web browser for the iPad, and I have a problem while I'm trying to make the history. Here's the code:
.m
-(void)viewDidLoad{
[self historyMethod];
//more settings...
}
-(void)historyMethod{
NSString *googleString= #"http://www.google.es";
NSString *currentURLString=TextField.text;
[historyArray addObject:googleString];
if ([googleString isEqual: currentURLString]) {
googleString = nil;
[TableView reloadData];
}
[historyArray addObject: currentURLString ];
[TableView reloadData];
}
The problem that I have is that the historyMethod is executed just one time, and I need it working all the time! Since I'm a newbie, I don't know too much about methods, and how I can make it work well. I tried it with a while loop but it didn't work. Please help me people!
You mean you want the method to be called repeatedly?
you can use something like NSTimer to repeat a method call;
[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(historyMethod) userInfo:nil repeats:YES];
This will call your method every 1 seconds, place this in your viewDidLoad method, instead of calling your historyMethod.
However, if you don't understand methods, I'd recommend some looking for some Objective-C tutorials.
If you're using UIWebView then you might wanna make your controller a UIWebViewDelegate.
By implementing - (void)webViewDidFinishLoad:(UIWebView *)webView you could keep track
of pages being loaded. Inside this delegate method you can get URL by webview.request.URL
Take a look at:
UIWebViewDelegate
UIWebViewDelegateProtocol
NSURLRequest

Dynamically generated NSTimers in same UITableview?

I'm trying to implement a timer into a new application. One use case that is likely is to have more than one timer running at a time in the same view updating a UILabel or UIButton title text.
Does anyone have experience of doing this? what approach would you suggest.
The difficulty is when the timers fire off the same selector in short succession.
many thanks in advance
Nick
In .h file declare object:
NSTimer *myTimer1,myTimer2;
In .m file:
myTimer1 = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:#selector(nextPhoto) userInfo:nil repeats:YES];
myTimer2 = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:#selector(nextPhoto) userInfo:nil repeats:YES];
After the above invalidate the timers like this:
[myTimer1 invalidate];
[myTimer2 invalidate];
Try it like this.
If you do create repeating timers dynamically, you can run into problems with timer invalidation. I'd suggest you use performSelector:withObject:afterDelay: or performSelectorOnMainThread:withObject:waitUntilDone: methods instead.

How to nest multiple CA animations?

How to nest multiple CA animation like UIView animateWithDuration does? For example, I need to animate 6 animations where each next animation goes after previous one. So, with UIView animateWithDuration, every next animation is called from complete block. Does CA allows to use blocks and etc.? If no then how to perform nested sequential animations?
CAAnimation doesn't have a block-based API, but you could use its delegate method animationDidStop:finished: to chain multiple animations.
If you do this a lot, you may want to write your own block-based wrapper for this.
Alternatively to omz's answer, you can set up NSTimer objects to start the successive parts of the animation.
[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:#selector(legOne:) userInfo:nil repeats:NO];
[NSTimer scheduledTimerWithTimeInterval:4.0 target:self selector:#selector(legTwo:) userInfo:nil repeats:NO];
Where legOne: is a method that does the first part of the animation, etc.

iOs: UIImageView setImage not working while scrolling

I have an UITableView which has UIViews inside each cell (actually I'm using EasyTableView) and inside that view there are 3 UIImageViews changing images every 1/3 of a second.
The problem is, that the images change only while there's no scrolling happening.
I read some issues about this and I found people suggesting the use of NSRunLoop, but that's for NSURLConnection when loading external images, I'm using "UIImage imageNamed" and UIImageView's setImage. The other suggestion I read was to use NSInvocationOperation, but had no luck with that either.
NSInvocationOperation *invocation = [[NSInvocationOperation alloc] initWithTarget:self selector:#selector(changeProgress) object:nil];
[invocation start];
Maybe I'm doing it wrong, please help! Thanks.
Did you try this?
UIScrollView redrawing content while scrolling?
A friend found the solution, I had to create the timer without scheduling it, and add it to the NSRunLoop, so the code would be like this:
_timer = [NSTimer timerWithTimeInterval:1.0/3.0 target:self selector:#selector(update:) userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];

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.
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.
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.
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];
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.