-(void) Downloadimages {
NSString *filedir = Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES). LastObject;
stringbyappendingpathcomponent:@ "image"];
dispatch_queue_t globalqueue = dispatch_get_global_queue (dispatch_queue_priority_default, 0);
_imggroup = Dispatch_group_create ();
NSLog (@ "start downloading");
Dispatch_group_async (_imggroup, Globalqueue, ^{
Dispatch_group_enter (_imggroup);
NSString *filepath = [Filedir stringbyappendingpathcomponent:@ "image0.jpg"];
[Self Downloadimagetopath:filepath url:self.imgurls[0]];
});
Dispatch_group_async (_imggroup, Globalqueue, ^{
Dispatch_group_enter (_imggroup);
NSString *filepath = [Filedir stringbyappendingpathcomponent:@ "image1.jpg"];
[Self Downloadimagetopath:filepath url:self.imgurls[1]];
});
Dispatch_group_async (_imggroup, Globalqueue, ^{
Dispatch_group_enter (_imggroup);
NSString *filepath = [Filedir stringbyappendingpathcomponent:@ "image2.jpg"];
[Self Downloadimagetopath:filepath url:self.imgurls[2]];
});
Dispatch_group_notify (_imggroup, Globalqueue, ^{
NSLog (@ "Finish download");
});
}
-(void) Downloadimagetopath: (NSString *) FilePath URL: (NSString *) url{
Afurlsessionmanager *sessionmgr = [[Afurlsessionmanager alloc] initWithSessionConfiguration:self.sessionConfig];
Nsurl *imgurl = [Nsurl Urlwithstring:url];
Nsurlrequest *request = [Nsurlrequest Requestwithurl:imgurl];
Nsurlsessiondownloadtask *downloadtask =
[Sessionmgr Downloadtaskwithrequest:request
Progress:nil
Destination:^nsurl * _nonnull (Nsurl * _nonnull TargetPath, Nsurlresponse * _nonnull response) {
Nsurl *documenturl = [Nsurl Fileurlwithpath:filepath];
return documenturl;
} completionhandler:^ (Nsurlresponse * _nonnull response, Nsurl * _nullable filePath, Nserror * _nullable error) {
Dispatch_group_leave (_imggroup);
}];
[Downloadtask resume];
}
IOS gcd with afnetworking asynchronous download