目标 - C从Web问题下载图像

时间:2011-06-16 12:47:37

标签: iphone objective-c ios image download

我使用此代码从服务器下载图像:

- (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中供查看。

谢谢,

杰克

2 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分:1)

  

然而我的问题是,如果我是   增加图像数量   比方说,20,会有很长的   等待图像下载和   然后显示。

NSURLConnection仅允许每个服务器同时连接6个。您应该使用NSOperationQueue并致电setMaxConcurrentOperationCount来设置小于6的值。

ASIHTTPRequest实现此方法,最多包含4个并发操作(如果代码中的其他内容正在发出请求)。请参阅this unit test中的示例。