简体   繁体   English

将图像存储到iPhone有滞后

[英]Store Image to iPhone with lag

I'm developing an iOS app that needs to download an image to display it the next time if the app has no internet connection. 我正在开发一个iOS应用程序,如果应用程序没有互联网连接,下次需要下载图像才能显示它。

I get the URL to download this image from a request to a server, I get a json back with a field "image" with the url of the image. 我得到了从请求下载此图像到服务器的URL,我得到一个带有图像url的字段“image”的json。 To download and store this image I do the following: 要下载并存储此图像,请执行以下操作:

NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:utfString]];
[data writeToFile:[NSString stringWithFormat:@"%@/%@",documentsDirectoryPath,@"superPremio"] atomically:NO];

This is called inside a successfully server call block. 这在成功的服务器调用块内部调用。 So I'm not doing this synchronously. 所以我不是同步这样做的。 This server call is being made in the viewDidLoad of the view controller that needs to display the image. 此服务器调用正在视图控制器的viewDidLoad ,该视图控制器需要显示图像。 This view controller has a UIScrollView and this scroll view is experimenting a lag when the image is written to the memory. 此视图控制器具有UIScrollView,此滚动视图正在尝试将图像写入内存时的延迟。 Everything is done in background processing so I'm not sure why this is happening... any idea? 一切都是在后台处理中完成的,所以我不确定为什么会发生这种情况......任何想法?

Thanks! 谢谢!

The data fetching process 数据获取过程

NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:utfString]];

is not asynchronous and it is causing trouble of lagging the UI 不是异步的,它导致了滞后UI的麻烦

Use this answer for making it asynchronous 使用此答案使其异步

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM