iOS development about TableView loading pictures when the card appears immediately solution

Source: Internet
Author: User

Novice do the development of the general will encounter the use of TableView from the internet to load pictures and display graphics and text when there will be cotton phenomenon, and this cotton phenomenon is due to a variety of reasons. Generally can be divided into two: one is because the online loading pictures need to dynamically define the height of the cell (if it is fixed height, can be ignored), and in the definition of cell height generally will first download the picture and then calculate the size of the picture, and then calculate the height of the cell. The other is that when the cell shows the picture, there is no asynchronous processing to load the picture and display the logic.

The following for the above two causes of cotton reasons for my method, for the first can be taken to capture the URL of the HTTP header information, calculate the height of the cell only to get the size of the picture, not to download pictures. This eliminates the time-consuming process of downloading a picture, but only when the picture is displayed in the cell. Here's how the picture type is jpg:

#pragma mark-#pragma mark jpg picture according to the picture partial data gets the picture's size + (cgsize) Downloadjpgimage: (nsstring*) strURL {nsmutableurlrequ
    EST *request = [[Nsmutableurlrequest alloc] Initwithurl:[nsurl Urlwithstring:strurl]];
    [Request setvalue:@ "bytes=0-209" forhttpheaderfield:@ "Range"];
    NSData *data = [nsurlconnection sendsynchronousrequest:request returningresponse:nil Error:nil];
return [self jpgimagesizewithheaderdata:data];
    } + (Cgsize) Jpgimagesizewithheaderdata: (NSData *) Data {if ([data length] <= 0x58) {return cgsizezero;
        } if ([Data length] < 210) {//definitely only one DQT field short w1 = 0, w2 = 0;
        [Data getbytes:&w1 Range:nsmakerange (0x60, 0x1)];
        [Data getbytes:&w2 Range:nsmakerange (0x61, 0x1)];
        Short w = (W1 << 8) + w2;
        Short H1 = 0, h2 = 0;
        [Data getbytes:&h1 Range:nsmakerange (0x5e, 0x1)];
        [Data Getbytes:&h2 Range:nsmakerange (0x5f, 0x1)];
        Short h = (H1 << 8) + H2; ReTurn Cgsizemake (W, h);
        else {short Word = 0x0;
        [Data Getbytes:&word Range:nsmakerange (0x15, 0x1)];
            if (Word = = 0xdb) {[Data Getbytes:&word range:nsmakerange (0x5a, 0x1)];
                if (Word = = 0xdb) {//two DQT field short w1 = 0, w2 = 0;
                [Data getbytes:&w1 Range:nsmakerange (0XA5, 0x1)];
                [Data getbytes:&w2 Range:nsmakerange (0XA6, 0x1)];
                Short w = (W1 << 8) + w2;
                Short H1 = 0, h2 = 0;
                [Data getbytes:&h1 Range:nsmakerange (0XA3, 0x1)];
                [Data Getbytes:&h2 Range:nsmakerange (0XA4, 0x1)];
                Short h = (H1 << 8) + H2;
            Return Cgsizemake (W, h);
                else {//one DQT field short w1 = 0, w2 = 0;
                [Data getbytes:&w1 Range:nsmakerange (0x60, 0x1)];
                [Data getbytes:&w2 Range:nsmakerange (0x61, 0x1)]; Short W = (W1 << 8) + W2;
                Short H1 = 0, h2 = 0;
                [Data getbytes:&h1 Range:nsmakerange (0x5e, 0x1)];
                [Data Getbytes:&h2 Range:nsmakerange (0x5f, 0x1)];
                Short h = (H1 << 8) + H2;
            Return Cgsizemake (W, h);
        } else {return cgsizezero;
 }
    }
}
Other pictures can be referenced by the following methods:

The solution to the second reason is to use asynchronous loading, and of course first of all you have to know that we often use the network to load pictures of the library is sdwebimage. In addition, if you want to load pictures asynchronously, it is best to customize the cell, and the loading of the data is assigned in Customcell.

Load asynchronously:

Dispatch_async (Dispatch_get_global_queue (dispatch_queue_priority_default, 0), ^{
                    [Stimgview setImageWithURL:[ Nsurl Urlwithstring:strpicurls]];
                    [SELF.RETSTIMAGEVIEWBG Addsubview:stimgview];
                

Related Article

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.