views:

229

answers:

2

Hello ! every one.

  • I am having number of images with in my application. ( images more than 50 - approximately & it can extend according to client's need )
  • Each image are very large round about - 1024 x 768 & 150 dpi
  • Now, I have to add all this images in a scroll view & display it.

Ok, My question is as follows.

According to me there are two options of loading large images

  1. imageNamed:@""
  2. load asynchronously when viewDidLoad Called.

Which is more preferable ?


imgModel.image=[UIImage imageNamed:[dMain valueForKey:@"imgVal"]];

or like this.

    NSURL *ur=[[NSURL alloc] initFileURLWithPath:[[NSBundle mainBundle] pathForResource:lblModelName.text ofType:@"png"] isDirectory:NO];

    NSURLRequest *req=[NSURLRequest requestWithURL:ur cachePolicy:NSURLCacheStorageNotAllowed timeoutInterval:40];
    [ur release];
    NSURLConnection *con=[[NSURLConnection alloc] initWithRequest:req delegate:self];
    if(con){
        myWebData=[[NSMutableData data] retain];
    } else {
    }

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    [myWebData setLength: 0];
}

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [myWebData appendData:data];
}

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    [connection release];
}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection {
    NSLog(@"ImageView Ref From - %@",imgV);
    // my image view & set image  
    imgV.image=[UIImage imageWithData:myWebData];
    [connection release]; connection=nil;
}
A: 

Asynchronous loading is almost always preferred to prevent unresponsive user interfaces (especially in viewDidLoad).

Furthermore, if you have a large number of images (and they are large files), you probably only want to load the images that will be visible (plus some buffer) and load the additional images when they are scrolled to.

Kevin Sylvestre
+2  A: 

Are you aware that the images won’t fit into memory all at once? If you want to scroll between them, you have to page them in and out to keep a decent memory footprint.

As for loading them, you should probably display a spinner, start loading the image in background and then replace the spinner with the image once it’s ready. Loading the image using NSURLConnection is overkill, you’ll probably have it easier with imageNamed:

- (void) startLoadingImage {
    // You need an autorelease pool since you are running
    // in a different thread now.
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    UIImage *image = [UIImage imageNamed:@"foo"];
    // All GUI updates have to be done from the main thread.
    // We wait for the call to finish so that the pool won’t
    // claim the image before imageDidFinishLoading: finishes.
    [self performSelectorOnMainThread:@selector(imageDidFinishLoading:)
        withObject:image waitUntilDone:YES];
    [pool drain];
}

- (void) viewDidLoad {
    UIActivityIndicator *spinner = …;
    [self.view addSubview:spinner];
    [self performSelectorInBackground:@selector(startLoadingImage)
         withObject:nil];
}

- (void) imageDidFinishLoading: (UIImage*) image {
    // fade spinner out
    // create UIImageView and fade in
}

The method names are spelled from memory, I might have missed a param here or there. But in principle the code is sound.

zoul
Perfect one. excellent.
sugar