iOs: UIImageView setImage not working while scrolling - iphone

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

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

How do they make those fancy Splash screens?

Although I currently only have one app out in the App store, I have several in the works and was wondering how users are making their splash screens.
I have seen several very cool animated ones and was wondering if this was all done via code or is it just something you would make in possibly iMovie and just run it as a video.
Any idea how some of these are being created? Examples are anything from Time Warner Cables app to Bejeweled.
Thanks in advance for the info.
Geo...
See iPhone Animated Loading Screen <-- the answer in there seems to be that the "fancy" splash screens aren't actually loading screens.
So what you will have to do is to create an animation (maybe a movie clip, or an imageview animation or similar) that can be run when the app starts artifically, possibly with you loading your resources behind that, rather than using the default splash screen functionality (to speed up the start of your app).
Hope that helps
Try this in App delegate class ....
- (void)applicationDidFinishLaunching:(UIApplication *)application {
UIImage *splashImage = [UIImage imageNamed:#"Picture 2.png"];
splashImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 20, 320, 480)];
splashImageView.contentMode = UIViewContentModeScaleAspectFit;
splashImageView.image = splashImage;
[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:#selector(removeSplashScreen) userInfo:nil repeats:NO];
[window addSubview:splashImageView];
}
-(void)removeSplashScreen{
[UIView beginAnimations: nil context:nil];
[UIView setAnimationDuration:2.0];
splashImageView.alpha = 0.0;
[UIView commitAnimations];
[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:#selector(releaseSplashScreen) userInfo:nil repeats:NO];
}
-(void)releaseSplashScreen{
[splashImageView removeFromSuperview];
[splashImageView release];
//Load the rootviewController here
}
You can also include Default.png in the resource of the project
There's a couple chapters on custom splash screens in Drance & Warren's book 'iOS Recipies' from Pragmatic Bookshelf.
Perhaps a commercial plug isn't what you're seeking (I'm not affiliated the the title or publisher), I just remember reading through it and finding it interesting.
You can do a fancy splash screen if you made it on the first view controller, like your real splash screen is the first scene of your animation, and the first view controller is the complete animation then you dismiss the first controller or you push the main view controller after a delay from you first view controller.

NSURLConnection threading problem

i have a big problem and i need your help. Here's what i need to accomplish:
The user select a row from a
TableView
A new view controller is pushed in
the NavigationController, and
displays only a "Loading" message
Meanwhile some data is read from an
XML file (via http)
When the data has been read, an
NSUConnection is used to load an
image from an URL (this URL is part
of the data)
While the image is still loading,
the other data is displayed on the
screen
The image has been downloaded and is
shown, completing the appearance of
the view
The big problem is that i can't use detachNewThreadSelector and NSURLConnection together!
So how can i make a workaround for this? How would you do this?
Thank you VERY much!
You can use following approach...(if you are using asynchronous request)
When your application comes in - (void)connectionDidFinishLoading:(NSURLConnection *)connection ... add a NSInvocationOperation object in NSOperationQueue (which you can handle at application level, by synthesizing it in appDelegate) ..
create NSInvocationOperation as follows..(in connectionDidFinishLoading)
NSInvocationOperation *operation = [[NSInvocationOperation alloc]initWithTarget:self selector:#selector(parseIt) object:nil];
[appDelegate.operationQueue addOperation:operation];
[operation release];
-(void) parseIt
{
//ask for parsing stuff....what you have earlier wrote directly in connectionDidFinishLoading
}
Thanks,
I would use a NSTimer to solve the problem using detachNewThreadSelector and NSURLConnection together.
I have similar scenario where there is a downloading Progress UIViewController showing till the file getting complete, here is what i do:
I Draw a loading View contains a Activity Indicator for example.
I initialize a NSTimer to keep checking if the file is complete.
I call the method that contains the Download Logic.
[1]
-(void) vManageFileRequest
{
[self.oFilesManager vGetSingleFileRequest];
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:#selector(vValidateFileRequest) userInfo:nil repeats:NO]];
}
[2]
[self performSelectorOnMainThread:#selector(vManageFileRequest) withObject:nil waitUntilDone:NO];

problem updating uiimageview from NSTimer method

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.

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.