1

(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.

}

目前显示的只是一个带有 labelLoadedString 变量“Radar”的白屏。

谢谢艾伦

4

2 回答 2

1

您的网址错误,缺少 SCHEME: ftp://


至于下载:

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];
}];
于 2013-01-01T03:35:55.000 回答
0

修复您的 URL(如前所述似乎不正确)后,检查 AFNetworking(请参阅https://github.com/AFNetworking/AFNetworking)。在处理从远程位置(例如通过 http、https、ftp 等)加载图像时,有一个类别UIImage将使您的生活更轻松;)

这是他们概述中的一个示例:

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"]];
于 2013-01-01T03:40:57.853 回答