Threading issues about uiimageview+afnetworking downloading pictures

Source: Internet
Author: User

Today wrote a demo, from the server to get pictures, and then show on the cell, we all know the cell reuse mechanism, when pulling down, the above cell is obscured, the following cell will reuse the masked cell,

Post code:

NSString *urlstring=[dict Objectforkey:@"ImagePath"]; Nsurl*url=[nsurl urlwithstring:urlstring];//gets the URL path of the pictureNsurlrequest *urlrequest=[[nsurlrequest Alloc]initwithurl:url CachePolicy: Nsurlrequestreloadignoringlocalandremotecachedata timeOutInterval:Ten]; NSLog (@"UI Thread is%@", [Nsthread CurrentThread]); //This is the main  thread//use uiimageview+afnetworking to download images asynchronously and cache them locallyDispatch_async (Dispatch_get_global_queue (Dispatch_queue_priority_high,0), ^{NSLog (@"Current Thread:%@", [Nsthread CurrentThread]);        //Use asynchronous mechanism here, definitely non-UI thread [Cell.myimage setimagewithurlrequest:urlrequest placeholderimage:[uiimage imagenamed:[dict obje Ctforkey:@"ImageName"]] Success:^ (Nsurlrequest *request, Nshttpurlresponse *response, UIImage *image) {NSLog (@"load Image thread is%@", [Nsthread CurrentThread]); //Guess what thread is here?            ? Dispatch_async (Dispatch_get_main_queue (),^{cell.myImage.image=image; NSLog (@"image thread:%@", [Nsthread CurrentThread]);                       //It must be the main thread}); NSLog (@"Download Image Success"); } Failure:^ (Nsurlrequest *request, Nshttpurlresponse *response, Nserror *error) {NSLog (@"picture loading failed:%@", error); }];

Just learn afnetworking, do not know who read the blog, said

Setimagewithurlrequest:urlrequest This method is an asynchronous mechanism, OK, I admit that it was simple, until today when I write this demo, I found that if not with the asynchronous mechanism, the downloaded image is very slow, and sometimes time out, It will be very jammed when I slide the cell, and then I'll print the thread to see if it's downloading something in the main thread and printing the result:


See
Setimagewithurlrequest:urlrequest This method of printing thread unexpectedly is the main thread, anyway I was surprised!!

Threading issues about uiimageview+afnetworking downloading pictures

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.