views:

16

answers:

1

Hello,

I have a application, and had to create another .h and .m file. This is so my downloads will be down in the background

.h File

#import <UIKit/UIKit.h>


@interface AsynchronousImageView : UIImageView
{
    NSURLConnection *connection;
    NSMutableData *data;
}

- (void)loadImageFromURLString:(NSString *)theUrlString;

@end

And .m file

#import "AsynchronousImageView.h"
#import "DxxxAppDelegate.h"



@implementation AsynchronousImageView

- (void)loadImageFromURLString:(NSString *)theUrlString
{
    [self.image release], self.image = nil;
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:theUrlString]
                                             cachePolicy:NSURLRequestReturnCacheDataElseLoad
                                         timeoutInterval:30.0];

    connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
}

- (void)connection:(NSURLConnection *)theConnection
    didReceiveData:(NSData *)incrementalData
{
    if (data == nil)
        data = [[NSMutableData alloc] initWithCapacity:2048];

    [data appendData:incrementalData];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)theConnection
{
    self.image = [UIImage imageWithData:data];
    [data release], data = nil;
    [connection release], connection = nil;
}

- (void)dealloc {
    [data release];
    [connection release];
    [super dealloc];
}

@end

Basicilly, i imported my .h file for my main xxx.appdelege so i could access a few of the Navigation COntrollers so i can push views.

Im basiclly trying to, download a picture, then once its completed display a view(loading view) when the download starts and remove view(loading screen) and push the main view with the UIImage.view is held.

I found these files online, they work but i cant find how i can do this. Any options?

Im trying to make it so, once it starts, i push a view using for loading, then i unload that screen and push the below

[navigationController pushViewController:vFullscreen animated:YES];

Can someone assist me? I went down this path because NSThread locked up because i tried to access UI things.

Thanks

+1  A: 

when you work with UI from background thread you should call [self performSelectorOnMainThread:@selector() withObject: waitUntilDone:]

when you are trying to make something in background - you should split UI logic and background logic, to make calls on main thread rarely.

for example

in main thread:

  • open loading view controller
  • set callback target and selector for background downloader
  • start downloading with with background downloader class
  • wait for callback from background downloader
  • show next viewController

in background thread (background downloader)

  • create thread
  • start downloading
  • when download finish - perform callback selector on main thread.

this logic can be useful many times for any object that you will need to download.

p.s. check apple source examples there is a many useful templates for common situations.

mOlind
Thanks :) Ill rewrite the code to see if i can do it this way instead.