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