2013-01-01 92 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"]]; 
相關問題