2013-01-01 66 views
1

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

- (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”的白色屏幕。

感谢 艾伦

+0

xcode是一个IDE只有 –

+0

我相信@ Daij-Djan意思是你标记了这个错误。将来,请标记为“iOS”,“objective-c”或其他类似建设性的内容。虽然这实际上并不能帮助回答这个问题,不是吗? ;) –

+0

我发现99%的ppl做错了^^对不起,如果我太短:D(我的答案应该更有帮助) –

回答

1

您的网址是错误的,它缺少的方案如下: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]; 
}]; 
+0

Thankyou太多了,它现在正在工作。我将尽力在将来标记任何问题。再次感谢艾伦 – user1908962

+0

:D很高兴我能帮助,新年快乐:D –

0

固定您的网址,之后作为提到似乎是不正确的,结账AFNetworking(见https://github.com/AFNetworking/AFNetworking)。有一个关于UIImage类别从远程位置加载图片的时候,这将使你的生活变得更加简单(如通过HTTP,HTTPS,FTP,你的名字;)

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

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"]]; 
相关问题