简体   繁体   English

将图像保存在文档目录中

[英]Saving images in Document Directory

I am downloading images from web in uitableview and want to save in document directory. 我正在uitableview中从Web下载图像,并希望保存在文档目录中。 I am downloading syncronously. 我正在同步下载。 I downloaded and saved in document directory successfully, but when i scroll uitable every time images are being saved , i want to save images at the time of downloading only once. 我已成功下载并保存在文档目录中,但是每次保存图像时滚动uitable时,我只想在下载一次时保存图像。 What should i do? 我该怎么办? Can anybody provide me some code? 有人可以给我一些代码吗? Any help will be appreciated. 任何帮助将不胜感激。

You should never use synchronous download . 永远不要使用同步下载 It will block your UI, make your UITableView scrolling lag, make the user think your app has frozen... Very bad idea. 它将阻止您的UI,使UITableView滚动滞后,使用户认为您的应用程序已冻结...这是个坏主意。

You should instead always use asynchronous download using NSURLConnection 's loadRequest: method and implementing the delegate methods yourself (even probably dedicate a class to manage the download, as everybody do) 相反,您应该始终使用NSURLConnectionloadRequest:方法并自己实现委托方法来使用异步下载(甚至可能像每个人一样专用于一个类来管理下载)

See the LazyTableImages sample code from Apple Documentation, it explains how to load images asychronously to display them in a TableView, loading them on demand, and avoiding to freeze the UI. 请参阅 Apple文档中的LazyTableImages示例代码,它说明了如何异步加载图像以将其显示在TableView中,按需加载并避免冻结UI。

Never do blocking operations in the delegate methods of UITableView like tableView:cellForRowAtIndexPath: (and I hope you use the reuse-mechanism of UITableViewCells correctly too, to reduce cell allocations. See the TableView Programming Guide about this point) 绝对不要在UITableView的委托方法中进行阻塞操作 ,例如tableView:cellForRowAtIndexPath:并且我希望您也正确使用UITableViewCells的重用机制,以减少单元分配。有关这一点,请参见《 TableView编程指南》)

Once you have implemented the asynchronous download using NSURLConnection and its delegate methods, its a piece of cake to move the code that saves the images in the documents folder in the connectionDidFinishLoading: delegate method (thus saving them only once and only when they are downloaded) 一旦使用NSURLConnection及其委托方法实现了异步下载,便可以轻而易举地移动将图像保存在connectionDidFinishLoading:委托方法的documents文件夹中的代码(因此,仅在下载时将它们保存一次)

Do the saving of images in didConnectionFinishLoading method. 用didConnectionFinishLoading方法保存图像。 You can also use an NSNotification for this, to know when the download is getting completed. 您也可以为此使用NSNotification,以了解下载何时完成。 Hope this information is useful. 希望此信息有用。

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

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