2012-05-05 66 views
0

我明白當連接到3G網絡時下載圖像時需要圖像壓縮,但我看起來真的很糟糕的圖像...我緩存下載的圖像,我意識到質量的圖像取決於活動連接。我的代碼:UIImage與NSData - 圖像壓縮依賴

 KTMember *member = [[DataManager sharedManager] getMemberWithId:memberId]; 
    if (member) { 
     NSLog(@"caching member %d locally",member.memberId); 
     memberImg = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:member.imageUrl]]]; 
     [[DataManager sharedManager] saveImageToDocuments:memberImg withId:memberId]; 
     return memberImg; 
    } else { 
     return nil; 
    } 

所以問題是 - 是否有任何方式覆蓋圖像壓縮,即使活動網絡是3G?

謝謝

回答

1

沒有適應性地增加緩慢連接的圖像壓縮的全局機制。您描述的內容需要服務器上的自定義代碼,並且因服務器而異。

什麼服務提供這些圖像?

+0

謝謝鄧肯。我從靜態URL下載JPEG圖像,我認爲這很難被認爲是「服務」。也許有些第三方應用程序造成這種情況?奇怪... – Stavash

0

編輯:感謝您解決我的答案,Verizon網絡優化有一些圖像壓縮機制。

我想,在字節流的術語中,圖像的質量取決於服務器是否提供壓縮。

但有一些解決方案。您還可以使用線程編程實現NSURLConnectionDataDelegate以處理來自URL請求的數據。有有趣的方法:

/** connection:didReceiveResponse: is called when 
*    enough data has been read to construct an 
*    NSURLResponse object. In the event of a protocol 
*    which may return multiple responses (such as HTTP 
*    multipart/x-mixed-replace) the delegate should be 
*    prepared to inspect the new response and make 
*    itself ready for data callbacks as appropriate. 
**/ 

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response; 

/** connection:didReceiveData: is called with a single 
    *    immutable NSData object to the delegate, 
    *    representing the next portion of the data loaded 
    *    from the connection. This is the only guaranteed 
    *    for the delegate to receive the data from the 
    *    resource load 
    **/ 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data; 

/** connection:willCacheResponse: gives the delegate 
    *    an opportunity to inspect and modify the 
    *    NSCachedURLResponse which will be cached by the 
    *    loader if caching is enabled for the original 
    *    NSURLRequest. Returning nil from this delegate 
    *    will prevent the resource from being cached. Note 
    *    that the -data method of the cached response may 
    *    return an autoreleased in-memory copy of the true 
    *    data, and should not be used as an alternative to 
    *    receiving and accumulating the data through 
    *    connection:didReceiveData 
    **/ 

- (NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse; 

/** connectionDidFinishLoading: is called when all 
    *    connection processing has completed successfully, 
    *    before the delegate is released by the 
    *    connection 
    **/ 

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

您還可以累加管理didReceiveData數據每個傳入數據,並在完成下載,在connectionDidFinishLoading,您可以處理圖像的接收到的所有的NSData

希望它可以幫助你。

+0

有報道稱,Verizon通過3G重新壓縮圖片。請參閱http://support.verizonwireless.com/information/data_disclosure.html通過http://arstechnica.com/civis/viewtopic.php?f=9&t=1165293 – EricS

+0

有趣。那篇文章只提到了視頻優化,但是,並不是圖像優化。作爲一名應用程序開發人員,如果您爲半永久性存儲和重複使用提供內容,那麼這種優化會令人生氣。 –