Goal - C Download Images from a Web Release

I have this code that I use to download images from the server:

- (void) loadDataWithOperation {

//Connection test
NSURL *url = [NSURL URLWithString:@"http://myurl.com/testconnection.php"];
NSError* error; 
NSString* connected = [NSString stringWithContentsOfURL:url encoding:NSASCIIStringEncoding error:&error];

//If the string Connected has NOT manged to initialise itself with the contents of the URL:
if (connected == NULL) {
    //Display error picture:

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error!" message:@"Error downloading gallery, please check network connection and try again."  delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
    [alert show];
    [alert release];

    [self createBackButton];

} else {
    //Load Data
    NSLog(@"Connected - %@",connected);

    NSLog(@"loadDataWithOperartion");
    //Create an array to hold the URLS
    NSMutableArray *myURLS;

    //Initialize the array with nil
    myURLS = [[NSMutableArray alloc] init];

    NSLog(@"Here1");
    //Add all the URLs from the server to the array
    for (int i = 0; i <= 4; i++){
        NSString *tempString = [[NSString alloc] initWithFormat : @"http://myurl.com/GalleryImages/%djack1.jpg", i];
        [myURLS addObject: [NSURL URLWithString:tempString]];
        [tempString release];
    }

    //Array to hold the image data
    NSMutableArray *myData;

    //Initialize the array with nil
    myData = [[NSMutableArray alloc] init];

    NSLog(@"Here2");
    //Add all the URLs from the server to the array
    for (int i = 0; i <= 4; i++){
        [myData addObject: [[NSData alloc] initWithContentsOfURL: [myURLS objectAtIndex:i]]];
    }

    //Array to hold the image data
    NSMutableArray *myImages;

    //Initialize the array with nil
    myImages = [[NSMutableArray alloc] init];

    NSLog(@"Here3");
    //Add all the URLs from the server to the array
    for (int i = 0; i <= 4; i++){
        [myImages addObject: [UIImage imageWithData: [myData objectAtIndex:i]]];
    }

    // Load an array of images into the page view controller
    //Initialising them with the data stored above
    NSArray *array = [[NSArray alloc] initWithArray:myImages];
    [self setImages:array];

    //Release the image data
    [myURLS release];
    [myData release];
    [myImages release];
    [array release];
}

}

This code works as it always loads a given number of images on the server, in this case 4.

However, my problem is that if I wanted to increase the number of images to say 20, it would be a very long time to wait for the images to load and then display.

Basically, I would like to upload 5 images from the loading screen, and then the remaining 15 downloads until the user can view the first 5. Can someone give me an idea of ​​how I will do this?

When images are downloaded, they are scaled to the iphone screen size and placed in the UIScrollView for viewing.

Thank,

Jack

+3
2

:

-(void)imageDownloadStart
{
[self performSelectorInBackground:@selector(downloadImages) withObject:nil];
}



-(void)downloadImages
{
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];

//WRITE YOU DOWNLOADING CODE HERE

if(yourCondition)
  {
  [self performSelectorOnMainThread:@selector(imageDownloadStart) withObject:nil waitUntilDone:YES];
  } 
[pool release];
}

:

-(UIImage *)resizeImage:(UIImage *)image withSize:(CGSize)newSize
{
float actualHeight = image.size.height;
float actualWidth = image.size.width;
float imgRatio = actualWidth/actualHeight;
float maxRatio = newSize.width/newSize.height;

if(imgRatio!=maxRatio)
{
    if(imgRatio < maxRatio){
        imgRatio = newSize.width / actualHeight;
        actualWidth = round(imgRatio * actualWidth);
        actualHeight = newSize.width;
    }
    else{
        imgRatio = newSize.height / actualWidth;
        actualHeight = round(imgRatio * actualHeight);
        actualWidth = newSize.height;
    }
}
CGRect rect = CGRectMake(0.0, 0.0, actualWidth, actualHeight);
UIGraphicsBeginImageContext(rect.size);
[image drawInRect:rect];
UIImage *resizedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//[resizedImage release];
return resizedImage;
}
+2

, , 20, .

NSURLConnection 6 . NSOperationQueue setMaxConcurrentOperationCount, 6.

ASIHTTPRequest ( , - ). . unit test.

+1

All Articles