簡體   English   中英

帶有NSData的UIImage-圖像壓縮依賴性

[英]UIImage with NSData - image compression dependancy

我知道在連接到3G網絡時下載圖像時需要圖像壓縮,但是圖像看起來真的很糟...我正在緩存下載的圖像,並且意識到圖像的質量取決於活動的連接。 我的代碼:

        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,是否有任何方法可以覆蓋圖像壓縮?

謝謝

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

哪些服務提供這些圖像?

編輯:感謝您修復我的答案,有一些通過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

希望對您有幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM