Objective - C Downloading Images from the Web Issue
I have this code that I use to download images from a server:
- (void) loadDataWithOperation {
//Connection test
NSURL *url = [NSURL URLWithString:@"http://myurl.com/testconne开发者_高级运维ction.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 there is always a set amount of images on the server to download, in this case 4.
However my problem is this, if I was to increase the amount of images to say, 20, there would be a very long wait while the images download and then display.
Basically I would like to download say 5 images with the loading screen, then have the remaining 15 download while the user is able to view the first 5. Can anyone give me any insight as to how I would do this?
When the images are downloaded they are scaled to the size of the iphone screen and placed in a UIScrollView for viewing.
Thanks,
Jack
To download image in background:
-(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];
}
And to resize:
-(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;
}
However my problem is this, if I was to increase the amount of images to say, 20, there would be a very long wait while the images download and then display.
NSURLConnection
only allows 6 simultaneous connections to each server. You should use a NSOperationQueue
and call setMaxConcurrentOperationCount
to set a value less than 6.
The library ASIHTTPRequest
implements this approach with a maximum of 4 concurrent operations (in case something else in your code is making a request). See an example in this unit test.
精彩评论