Ios 带NSData的UIImage-图像压缩依赖性
我理解在连接到3G网络时下载图像时需要进行图像压缩,但我得到的图像非常难看。。。我正在缓存下载的图像,我意识到图像的质量取决于活动连接。我的代码:Ios 带NSData的UIImage-图像压缩依赖性,ios,cocoa-touch,uiimage,nsdata,Ios,Cocoa Touch,Uiimage,Nsdata,我理解在连接到3G网络时下载图像时需要进行图像压缩,但我得到的图像非常难看。。。我正在缓存下载的图像,我意识到图像的质量取决于活动连接。我的代码: KTMember *member = [[DataManager sharedManager] getMemberWithId:memberId]; if (member) { NSLog(@"caching member %d locally",member.memberId); member
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
中管理您的数据,并累积每个传入数据,完成下载后,在connectiondifinishload
中,您可以处理您接收到的所有图像的NSData
希望对您有所帮助。有报道称Verizon通过3G重新压缩图像。通过有趣的方式查看。这篇文章只提到了视频优化,而不是静态图像优化。作为一名应用程序开发人员,如果你交付内容用于半永久性存储和重复使用,那么这种优化会让人抓狂。谢谢Duncan。我从静态URL下载jpeg图像,我认为这很难被视为一项“服务”。可能是第三方应用程序造成的?奇怪的