2011-03-21 40 views
0

我正在使用TTStyledTextLabel和來自XHTML數據的TTStyledText來顯示新聞文章。 它工作正常,除了img是數據而不是鏈接,在這種情況下它崩潰!Three20 - TTStyledTextLabel遇到base64圖像數據而不是URL - 崩潰

代碼

TTStyledTextLabel *storyLabel = [[TTStyledTextLabel alloc] init]; 
[storyLabel setText: [TTStyledText textFromXHTML:[articleContents objectForKey:@"storyText"]]]; 

正常工作與正常的圖像網址XML,

但是當它遇到這樣的圖像數據:

img class="alignleft" src="data:image/jpg;base64,/9j/4AAQSkZJRgA... 
(lots more in here)...1HhI0T//2Q==" alt="" width="267" height="189"/

它與輸出崩潰:

-[NSURLResponse allHeaderFields]: unrecognized selector 
sent to instance 0xb83b370 

這隻發生在遇到圖像數據時,否則如果是正常的img鏈接,它會加載正常。

謝謝!

回答

0

在這裏回答我自己的問題。

這似乎是一個罕見的情況發生(在我的具體情況)。我解決這個問題的方式是通過手動檢查來查看img鏈接(它可能是圖像數據)是否具有常見的圖像文件類型擴展名(jpg,png,gif等)。如果不存在並丟棄數據,我只是無視它。

我對Web標準不太瞭解,以及是否認爲可以將圖像數據嵌入到鏈接應該存在的地方,但是現在我意識到它存在並可能導致使用此類的崩潰。希望這可以幫助任何有此問題的人。

2

問題是Three20 TTRequestLoader在NSURLConnectionDataDelegate方法中使用NSHTTPURLResponse而不是NSURLResponse。 NSURLResponse沒有allHeaderFields方法,所以應用程序崩潰。

- (void)connection:(NSURLConnection*)connection didReceiveResponse:(NSHTTPURLResponse*)response { 
    _response = [response retain]; 

    if ([response isKindOfClass:[NSHTTPURLResponse class]]) { 
    NSDictionary* headers = [response allHeaderFields]; 
    int contentLength = [[headers objectForKey:@"Content-Length"] intValue]; 

    // If you hit this assertion it's because a massive file is about to be downloaded. 
    // If you're sure you want to do this, add the following line to your app delegate startup 
    // method. Setting the max content length to zero allows anything to go through. If you just 
    // want to raise the limit, set it to any positive byte size. 
    // [[TTURLRequestQueue mainQueue] setMaxContentLength:0] 
    TTDASSERT(0 == _queue.maxContentLength || contentLength <=_queue.maxContentLength); 

    if (contentLength > _queue.maxContentLength && _queue.maxContentLength) { 
     TTDCONDITIONLOG(TTDFLAG_URLREQUEST, @"MAX CONTENT LENGTH EXCEEDED (%d) %@", 
         contentLength, _urlPath); 
     [self cancel]; 
    } 

    _responseData = [[NSMutableData alloc] initWithCapacity:contentLength]; 
    } 
    else if ([response isKindOfClass:[NSURLResponse class]]) { 
    _responseData = [[NSMutableData alloc] init]; 
    } 
    else { 
    [self cancel]; 
    } 
} 

- (void)connectionDidFinishLoading:(NSURLConnection *)connection { 
    TTNetworkRequestStopped(); 

    if ([_response isKindOfClass:[NSHTTPURLResponse class]]) { 
    TTDCONDITIONLOG(TTDFLAG_ETAGS, @"Response status code: %d", _response.statusCode); 

    // We need to accept valid HTTP status codes, not only 200. 
    if (_response.statusCode >= 200 && _response.statusCode < 300) { 
     [_queue loader:self didLoadResponse:_response data:_responseData]; 
    } else if (_response.statusCode == 304) { 
     [_queue loader:self didLoadUnmodifiedResponse:_response]; 
    } else { 
     TTDCONDITIONLOG(TTDFLAG_URLREQUEST, @" FAILED LOADING (%d) %@", 
         _response.statusCode, _urlPath); 
     NSError* error = [NSError errorWithDomain:NSURLErrorDomain code:_response.statusCode 
             userInfo:nil]; 
     [_queue loader:self didFailLoadWithError:error]; 
    } 
    } 
    else if ([_response isKindOfClass:[NSURLResponse class]]) { 
    [_queue loader:self didLoadResponse:_response data:_responseData]; 
    } 

    TT_RELEASE_SAFELY(_responseData); 
    TT_RELEASE_SAFELY(_connection); 
} 

您可以通過檢查類修復