How to download & display an image from ftp server

时间:2022-06-01 20:35:25

(new to xCode and programming) I am trying to connect/download/save to variable and display an image in UIImageView from an ftp server. The code is below. Any help advice would be appreciated.

(xCode和编程新手)我正在尝试连接/下载/保存到变量并从ftp服务器在UIImageView中显示图像。代码如下。任何帮助建议将不胜感激。

- (void)viewDidLoad
{
    //sets label to notify user whats happening
    NSMutableString *labelString;
    labelString = [NSMutableString stringWithString: @"Connecting"];
    [labelDymamic setText: labelString];

    //variable for ftp location
    NSString *ftpLocation = [NSString stringWithFormat:@"ftp2.bom.gov.au/anon/sample/gms/IDE00003.201011170430.gif"];
    //variable to recieve data
    NSMutableData *responseData;

    //loads ftpLocation into url
    NSURL *url = [NSURL URLWithString:ftpLocation];

    //sets label to notify user whats happening
    NSMutableString *labelStringDownloading;
    labelStringDownloading = [NSMutableString stringWithString: @"Downloading"];
    [labelDymamic setText: labelStringDownloading];

    //Connect to ftp
    self.ftpImageView.image = [UIImage imageNamed:@"Icon.png"];
    self.labelDymamic.text = @"Receiving";
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    (void) [[NSURLConnection alloc] initWithRequest: request delegate:self];

    //download image


    //save to variable


    //display to screen


    //sets label to notify application loading complete
    NSMutableString *labelLoadedString;
    labelLoadedString = [NSMutableString stringWithString: @"Radar"];
    [labelDymamic setText: labelLoadedString];

    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

}

At the moment all that is displayed is a white screen with labelLoadedString variable "Radar".

目前所有显示的都是带有labelLoadedString变量“Radar”的白色屏幕。

Thanks Allan

谢谢艾伦

2 个解决方案

#1


1  

your url is wrong, its missing the SCHEME: ftp://

你的网址错了,它错过了SCHEME:ftp://


as for downloading:

至于下载:

NSURLRequest *request = [NSURLRequest requestWithURL:url];

//download
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * r, NSData * d, NSError * e) {
    //save
    UIImage *img = [[UIImage alloc] initWithData:d];

    //display
    self.imageView.image = img;

    //sets label to notify application loading complete
    NSMutableString *labelLoadedString;
    labelLoadedString = [NSMutableString stringWithString: @"Radar"];
    [labelDymamic setText: labelLoadedString];
}];

#2


0  

After fixing your URL, which as mentioned appears to be incorrect, checkout AFNetworking (see https://github.com/AFNetworking/AFNetworking). There's a category on UIImage that will make your life a lot easier when dealing with loading images from remote locations (such as over http, https, ftp, you name it ;)

在修复您的URL(如上所述)之后,请检查AFNetworking(请参阅https://github.com/AFNetworking/AFNetworking)。 UIImage上有一个类别,在处理从远程位置加载图像时会让您的生活变得更轻松(例如通过http,https,ftp,您可以命名;)

Here's an example from their overview:

以下是他们概述的一个例子:

UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 100.0f, 100.0f)];
[imageView setImageWithURL:[NSURL URLWithString:@"http://i.imgur.com/r4uwx.jpg"] placeholderImage:[UIImage imageNamed:@"placeholder-avatar"]];

#1


1  

your url is wrong, its missing the SCHEME: ftp://

你的网址错了,它错过了SCHEME:ftp://


as for downloading:

至于下载:

NSURLRequest *request = [NSURLRequest requestWithURL:url];

//download
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * r, NSData * d, NSError * e) {
    //save
    UIImage *img = [[UIImage alloc] initWithData:d];

    //display
    self.imageView.image = img;

    //sets label to notify application loading complete
    NSMutableString *labelLoadedString;
    labelLoadedString = [NSMutableString stringWithString: @"Radar"];
    [labelDymamic setText: labelLoadedString];
}];

#2


0  

After fixing your URL, which as mentioned appears to be incorrect, checkout AFNetworking (see https://github.com/AFNetworking/AFNetworking). There's a category on UIImage that will make your life a lot easier when dealing with loading images from remote locations (such as over http, https, ftp, you name it ;)

在修复您的URL(如上所述)之后,请检查AFNetworking(请参阅https://github.com/AFNetworking/AFNetworking)。 UIImage上有一个类别,在处理从远程位置加载图像时会让您的生活变得更轻松(例如通过http,https,ftp,您可以命名;)

Here's an example from their overview:

以下是他们概述的一个例子:

UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 100.0f, 100.0f)];
[imageView setImageWithURL:[NSURL URLWithString:@"http://i.imgur.com/r4uwx.jpg"] placeholderImage:[UIImage imageNamed:@"placeholder-avatar"]];