开发者

Saving images in Document Directory

I am downloading images from web in uitableview and want to save in document directory. I am downloading syncronously. I downloaded and saved in document directory succe开发者_开发百科ssfully, but when i scroll uitable every time images are being saved , i want to save images at the time of downloading only once. 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.

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)

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.

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)

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)


Do the saving of images in didConnectionFinishLoading method. You can also use an NSNotification for this, to know when the download is getting completed. Hope this information is useful.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜