【问题标题】:Objective - C Downloading Images from the Web IssueObjective - C 从 Web 问题下载图像
【发布时间】: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


    【解决方案1】:

    在后台下载图片:

    -(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 使用最多 4 个并发操作来实现此方法(以防您的代码中的其他内容发出请求)。请参阅this unit test 中的示例。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-04-30
        • 2012-03-27
        • 1970-01-01
        • 2011-06-29
        • 2013-01-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多