0

我了解在连接到 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,是否有任何方法可以覆盖图像压缩?

谢谢

4

2 回答 2

1

没有全局机制可以自适应地增加慢速连接的图像压缩。您所描述的内容需要服务器上的自定义代码,并且会因服务器而异。

提供这些图像的服务是什么?

于 2012-05-06T01:58:51.340 回答
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您收到的所有图像。

希望它可以帮助你。

于 2012-05-06T02:20:49.817 回答