2011-06-28 23 views
0

當我異步使用NSURLConnection嘗試獲取帶有圖像數據的NSData時,圖像將返回爲空白,但當我同步使用dataWithContentsOfURL時,我沒有問題,圖像數據正確。是否有任何理由爲什麼我的異步方法會失敗?使用NSURLConnection時圖像空白但使用dataWithContentsOfURL時不使用

這工作:

NSData *data = [NSData dataWithContentsOfURL: url]; 
NSLog(@"TEST %@", data); 
UIImage *map = [UIImage imageWithData:data]; 
mapView.image = map; 

這並不:

// 
// MapHttpRequest.m 
// GTWeb 
// 
// Created by Graphic Technologies on 6/21/11. 
// Copyright 2011 __MyCompanyName__. All rights reserved. 
// 

#import "MapHttpRequest.h" 

@implementation MapHttpRequest 
@synthesize receivedData; 
@synthesize dataString; 
@synthesize vc; 

- (void)request:(NSString *)url fromView:(UIViewController *) theVC 
{ 
vc = theVC; 
// Create the request. 
NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:url] 
              cachePolicy:NSURLRequestUseProtocolCachePolicy 
             timeoutInterval:60.0]; 
NSLog(@"URL: %@", url); 
// create the connection with the request 
// and start loading the data 
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; 
if (theConnection) { 
    // Create the NSMutableData to hold the received data. 
    // receivedData is an instance variable declared elsewhere. 
    receivedData = [[NSMutableData data] retain]; 
} else { 
    // Inform the user that the connection failed. 
} 
} 

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse    *)response 
{ 
// This method is called when the server has determined that it 
// has enough information to create the NSURLResponse. 

// It can be called multiple times, for example in the case of a 
// redirect, so each time we reset the data. 

// receivedData is an instance variable declared elsewhere. 
[receivedData setLength:0]; 
} 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{ 
// Append the new data to receivedData. 
// receivedData is an instance variable declared elsewhere. 
[receivedData appendData:data]; 
} 

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 
{ 
// release the connection, and the data object 
[connection release]; 
// receivedData is declared as a method instance elsewhere 
[receivedData release]; 

// inform the user 
NSLog(@"Connection failed! Error - %@ %@", 
     [error localizedDescription], 
     [[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]); 
} 

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 
// do something with the data 
// receivedData is declared as a method instance elsewhere 

[vc mapImageConnectionFinished:receivedData]; 

// release the connection, and the data object 
[dataString release]; 
[connection release]; 
[receivedData release]; 
} 

@end 
+0

這將有助於查看您使用NSURLConnection的代碼。 – highlycaffeinated

+0

@highlycaffeinated我已經編輯過它,我不小心打進了,並在我準備好之前貼出來。 – Millec8

+0

我看不到你在哪裏填充你創建的receivedData對象。 – highlycaffeinated

回答

0

原來,這是給我一個不好的要求,因爲有一個不可見的回車或空白區域。修剪它:

url = [url stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; 

解決了我的問題。

0

它看起來像你開始尋找在引導上http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html但沒有讀完吧:)

您需要實現將收到有關正在接收的數據信息的方法。

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 

它的所有描述在我提供的鏈接中。

+0

不,我已經實現了使用NSURLConnection所需的全部四種方法,我只是在實際請求中發佈了片段。如果你認爲這可能是一個問題,那麼我也可以發佈這些。 – Millec8

+0

是的請發佈完整的源代碼:) –

+0

好的全班發佈。 – Millec8

0

根據你的代碼,你沒有安排連接來運行:

[connection scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes]; 
[connection start]; 

你把你委託的回調的痕跡,以確保他們被稱爲?

+0

是的,我已在我的回調中放入了痕跡,並且所有內容都被調用。我不確定我需要執行scheduleInRunLoop,因爲我在其他地方使用相同的代碼來進行異步連接,這些異步連接只與文本正常工作。它只是圖像的NSData不能正確返回。 – Millec8

相關問題