Comprehensive application of asynchronous download images

Source: Internet
Author: User

1. The problem to be resolved.

Sets the picture that is downloaded from the network to the cell.

2. Ideas:

>1. Sync Download First

BUG: Downloading is a time-consuming operation that blocks the main thread

>2. Downloading images asynchronously

BUG: The picture has a wrong line.

> The picture requested from the network does not match the cell. Why is this? The root cause is the reuse of cells.

.

There are 3 cells on the TableView. Each cell has a picture, text. The text is loaded by plist, and when the cell is scrolled up, so that cell 1 is rolled out of the interface, then the 4th cell is loaded, and the fourth cell is still newly created, because when cell 1 has not scrolled out of view, Cell 4 is already out of the box and is impossible to reuse. When cell 4 is fully loaded, cell 1 is fully scrolled out of the interface, the cache pool has reusable cells, and when the 5th cell is loaded, it is not rewritten to create the cell, but to the cache pool, so the 1th cell is placed in the position of the 5th cell, If the data for cell fifth is not set according to Indexpath at this time, cell reuse occurs, and the 5th cell and the 1th cell are exactly the same, therefore, in order to resolve cell reuse, you need to reset the data for cell 5 based on Indexpath.

Because it is time-consuming to request data from the network, a situation may occur:

The picture of cell 1 has not yet been requested, it has scrolled out of the interface, cell 5 has reused cell 1 data, and can be reused as a result of using the corresponding text data in the plist file according to Indexpath. Cells also send requests to the network to request their own pictures, However, if cell 1 requests a slow picture, cell 5 sets its own picture and then sets the picture for cell 1, overwriting it. Causes the picture to not match the text.

So what happens when you set the text plist? First, I think if loading data from a plist file is as time consuming as requesting data from the network, and taking different strings time-consuming gaps, there will also be coverage issues. But I think it's very fast to fetch data from the local, and it's time consuming It is similar to a string of length 10, and a length of 100, from the plist. This error should not normally occur

Sdwebimage solution, that is, to cut off this multiple data filling a cell problem, when the cell has gone out, but did not download the good picture, then cancel the download operation of cell 1, and then roll to cell 5, this cell is only a network request, There will be no more slow Web requests than the pictures come back to overwrite the correct data corresponding to cell 5.

This implementation is cumbersome, first of all to consider a simple solution.

Mode 1: Do not directly set the data requested by the network to the cell, but by adding an attribute uiimage to the model corresponding to the plist file, because the model data is always corresponding, because the angle label is different, when the picture data of cell 1 is requested back, set to the Image property in the model, Because---------this is not the case, the request back to the image is set to the Image property of the model when the confusion? for example, cell 1 request data is slow, cell 3 requested the data set to cell 1 corresponding to the Image property? For the time being, it should not, Because in fact, each request picture is a new thread, if the CPU is allocated, then each cell corresponds to a thread, that is, cell 3 request data and cell 1 request the picture and set the picture is on a different thread, does not occur the idea of the problem. Assuming that the CPU does not allow each cell to re-thread, Instead of adding a new task to the same thread after the last execution, this confusion does not occur.

Because when the data is set, the child thread requests the picture, so it can directly ignore the code, resulting in a bug, the picture request back, but there is no picture, because the control is lazy loading, when the first layout of the cell, and no picture request back, Thus Layoutsubviews will not layout the position of the picture frame, when the slide or click on the cell, but also call the Layoutsubviews method, at this time the system realizes that there is a picture, thus the layout of the picture, the problem can be accounted for by the bitmap resolution,

Mode 2:

The idea and the front is still the same, do not directly associate the downloaded data and cell, through a mediation will download the picture and plist file one by one corresponding to. The dictionary is one by one corresponding, the URL in the model as a key, (each cell is different, and therefore unique), Store the picture as a value. At this time also need to download pictures from the picture cache dictionary, if there is, set the picture, no download, can also solve the wrong line problem. Sdwebimage is only through the Nscache solution, the realization of ideas is the same. and Nscache in fact, and the dictionary is similar, only need to setobjectforkey save, Objectforkey to take.

>3. A Web-enabled image needs to be cached locally, so that it can be taken to the cache when there is no net. Data localization can be used for archiving or plist, I use plist, note that when there is no net when you can find from the sandbox. And since the efficiency of the sandbox is not as high as it is from the memory, When you first load from the sandbox, and then swipe up and down, consider going from memory, and then you can remove the picture from the sandbox and set the picture to the picture cache. If it is the first time to load from the Web, and then swipe up and down, loading from the sandbox, instead of directly from the memory load efficiency, Consider downloading the same settings to the image cache (memory) when downloaded from the Internet.

>4.BUG3, even if the picture is downloaded, but again scrolling the cell will be downloaded again, this display is not necessary, my initial idea is to add a bool value to the model, when the task is added to the queue, the bool value is set to Yes, is downloading, do not have to repeat the download, It can actually solve the problem. But there is a problem. If the user clears the cache, and the program does not hang up, that is, the sandbox, there is no picture, then you can go to the picture cache, if re-run the program, the bool value and return to nil, and loaded from the Internet, seemingly no problem.???? Why do you want to remove the operation cache???? Is there a possibility that the bool value is displayed as being downloaded, but there is no data in the picture cache, there is no data in the sandbox, but because the bool value shows that the download is not possible to download again??? It is possible that when a memory warning is received, the memory is emptied, the cache is cleared in the sandbox, and the bool value in the model still shows that the download is being downloaded, which prevents it from being downloaded again.-----There is a problem, when a memory warning is received, the picture cache in the memory cache is emptied and the cache is cleared in the sandbox. When the action cache shows that the download is being downloaded, it will not be downloaded again, and if the bitmap is set up, only the bitmap will occur, and if no bitmap is set, the picture will be reused. Therefore, when the picture is downloaded, the operation cache needs to be removed.

>5. It is true that the bool value solves the problem of duplicate download, but will this result in too much coupling of the code? Suppose you need to extract a transaction other than the download, not in the controller, specifically encapsulated in a manager class, the operation cache this step how to encapsulate it?

>6. Before adding something new, optimize the current code, don't you think it's too verbose to set the cell?

Comprehensive application of asynchronous download images

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.