IOS download Web image method with GCD

Source: Internet
Author: User

Downloading a network picture with multithreading can cause repeated downloads of wasted traffic.

In order to avoid the above situation, using multi-threaded download image method can be used the following methods.

1. Create a dictionary, if downloaded the picture will be the image address is key, the picture is the value of the save.

2. Create a dictionary, if the child thread has started to download then use the image address as key, and the image address as value to save.

3. If you are downloading, display a placeholder picture

4. If there is a downloaded picture in the dictionary, refresh the picture

Property

@interface Viewcontroller () @property (weak, nonatomic) Iboutlet Uiimageview *imageviewoutlet; @property (Nonatomic, Strong) Nsmutabledictionary *imagedict, @property (nonatomic, strong) Nsmutabledictionary *dispatchdict; @property ( Nonatomic, assign) dispatch_queue_t globalqueue; @property (nonatomic, assign) dispatch_group_t dispatchgroup;@ Property (Nonatomic, Strong) NSData *imagedata; @end

Lazy Loading

-(Nsmutabledictionary *) imagedict {if (!_imagedict) {_imagedict = [nsmutabledictionary dictionary];} return _imagedict;} -(Nsmutabledictionary *) dispatchdict {if (!_dispatchdict) {_dispatchdict = [nsmutabledictionary dictionary];} return _dispatchdict;} -(dispatch_queue_t) Globalqueue {if (!_globalqueue) {_globalqueue = Dispatch_get_global_queue (DISPATCH_QUEUE_ Priority_default, 0);} return _globalqueue;}

Click on the screen to start downloading

-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *) event {[Self dispatchgroupasync];}

Download picture operation

-(void) Dispatchgroupasync {dispatch_group_t dispatchgroup = Dispatch_group_create (); NSString *imagename = @ "Http://pic29.nipic.com/20130424/11921802_221046126000_2.jpg"; UIImage *image = self.imagedict[imagename];//dictionary without picture if (image = = nil) {NSLog (@ "image is nil"); NSString *dispatchname = self.dispatchdict[imagename];//also not downloaded in if (dispatchname = = nil) {NSLog (@ "Dispatch is nil"); Dispatch_group_async (Dispatchgroup, Self.globalqueue, ^{//add thread dictionary self.dispatchdict[imagename] = imageName; Nsurl *url = [Nsurl urlwithstring:imagename];self.imagedata = [NSData datawithcontentsofurl:url];}); Dispatch_group_notify (Dispatchgroup, Self.globalqueue, ^{nslog (@ "dispatch_group_notify queue finished!"); [self.dispatchdict removeobjectforkey:imagename];self.imagedict[imagename] = self.imagedata; [Self Performselectoronmainthread: @selector (refreshimage) Withobject:nil waituntildone:yes];});} Also in the download else {self.imageViewOutlet.image = [UIImage imagenamed:@ "dufault.jpg"];}} There are already downloaded pictures else {NSLog (@ "refresh picture"); Self.imageviewOutlet.image = [UIImage imagewithdata:self.imagedict[imagename] scale:0.0];}} 

After downloading the picture, go back to the main thread to refresh the picture

-(void) refreshimage {self.imageViewOutlet.image = [UIImage imageWithData:self.imageData scale:0.0];}

Run effect

To test for preventing duplicate download operations, punch multiple screens in a row at the beginning of the download

2015-08-08 22:02:18.055 gcd download Image [2840:163865] image is nil2015-08-08 22:02:18.055 gcd download picture [2840:163865] Dispatch is nil2015-08-08 22:02:18.235 gcd download Image [2840:163865] image is nil2015-08-08 22:02:18.403 gcd download picture [2840:163865] image is nil2015-08-08 22:02:18.560 gcd download Image [2840:163865] image is nil2015-08-08 22:02:18.704 gcd download picture [2840:163865] image is nil2015-08-08 22:02:18.874 gcd download Image [2840:163960] dispatch_group_notify queue finished!2015-08-08 22:02:18.890 gcd download Image [ 2,840:163,865] Refresh picture 2015-08-08 22:02:19.495 gcd download picture [2,840:163,865] Refresh Picture

---end


Note: If you want to learn to download with nsoperation, please see http://blog.csdn.net/zhangwenhai001/article/details/46723597


Copyright NOTICE: This article is the original blogger article, reproduced please indicate the source: http://blog.csdn.net/zhangwenhai001

IOS download Web image method with GCD

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.