【发布时间】:2011-06-16 12:47:37
【问题描述】:
我有这段代码用于从服务器下载图像:
- (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];
}
}
此代码有效,因为服务器上总是有一定数量的图像要下载,在本例中为 4。
但是我的问题是,如果我要增加图像的数量,比如 20,图像下载然后显示会等待很长时间。
基本上,我想在加载屏幕上下载 5 张图片,然后在用户能够查看前 5 张的同时下载剩余的 15 张图片。谁能告诉我如何做到这一点?
下载图片后,它们会缩放到 iphone 屏幕的大小并放置在 UIScrollView 中以供查看。
谢谢,
杰克
【问题讨论】:
标签: iphone objective-c ios image download