【问题标题】:UIImage with NSData - image compression dependancyUIImage 与 NSData - 图像压缩依赖
【发布时间】:2012-05-06 16:12:00
【问题描述】:

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

谢谢

【问题讨论】:

    标签: ios cocoa-touch uiimage nsdata


    【解决方案1】:

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

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

    【讨论】:

    • 谢谢邓肯。我正在从静态 URL 下载 jpeg 图像,我认为这几乎不能被视为“服务”。也许是某些第三方应用程序导致了这种情况?奇怪...
    【解决方案2】:

    编辑:感谢您修复我的答案,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

    希望对你有帮助。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-22
    • 1970-01-01
    相关资源
    最近更新 更多