uicollectionview - How to increase the speed of loading images to UIImageView using AFNetworking in iOS -
i have collectionview , inside collectionview cell has uiimageview. used afnetworking load images web service.but takes more time load data.is there way speed process. have attached code. hope help.
//loading data
- (void)loadcategorydata { post = nil; nsstring *mainurl = [nsstring stringwithformat:@"some url"]; afhttprequestoperationmanager *manager = [afhttprequestoperationmanager manager]; manager.responseserializer.acceptablecontenttypes = [manager.responseserializer.acceptablecontenttypes setbyaddingobject:@"text/html"]; [manager get:mainurl parameters:nil success:^(afhttprequestoperation *operation, id responseobject) { posts = (nsdictionary *)responseobject; post = [nsmutablearray array]; for(nsdictionary *all in posts) { categories *category = [categories new]; category.title = [all objectforkey:@"catname"]; category.firsturl = [all objectforkey:@"url"]; [self.maincollectionview reloaddata]; //call images imagespost = nil; nsstring *imageurl = [nsstring stringwithformat:@"%@", category.firsturl]; afhttprequestoperationmanager *managerone = [afhttprequestoperationmanager manager]; [managerone get:imageurl parameters:nil success:^(afhttprequestoperation *operation, id responseobject) { imagesposts = (nsdictionary *)responseobject; nsarray *resultone = [imagesposts objectforkey:@"posts"]; imagespost = [nsmutablearray array]; if ([resultone count]) { nsdictionary *firstpost = resultone[0]; // categories *newmogocat = [categories new]; nsdictionary *thumbnail_images = [firstpost objectforkeyedsubscript:@"thumbnail_images"]; nsdictionary *thumbnail = [thumbnail_images objectforkey:@"thumbnail"]; category.imageoneurl = [nsstring stringwithformat:@"%@",[thumbnail objectforkey:@"url"]]; // nslog(@"%@", newmogocat.imageoneurl); // [imagespost addobject:newmogocat]; [post addobject:category]; [self.maincollectionview reloaddata]; } } failure:^(afhttprequestoperation *operation, nserror *error) { }]; } } failure:^(afhttprequestoperation *operation, nserror * responseobject) { }]; } // assign values collectionview cell
- (uicollectionviewcell *)collectionview:(uicollectionview *)collectionview cellforitematindexpath:(nsindexpath *)indexpath { categorycollectionviewcell *cell = [collectionview dequeuereusablecellwithreuseidentifier:@"cellidentifier" forindexpath:indexpath]; // cell.layer.borderwidth = 2.0f; cell.layer.maskstobounds = no; cell.layer.cornerradius = 5.0; cell.layer.bordercolor = [uicolor purplecolor].cgcolor; cell.layer.shadowoffset = cgsizemake(0, 1); cell.layer.shadowradius = 2.0; cell.layer.shadowcolor = [uicolor bluecolor].cgcolor; [cell addsubview:cell.maintitlelabel]; categories *cat = [post objectatindex:indexpath.row]; [self.maincollectionview reloadinputviews]; cell.maintitlelabel.text = [nsstring stringwithformat:@" %@ ", cat.title]; [cell.maintitlelabel sizetofit]; nsstring *mainstring = [nsstring stringwithformat:@"%@", cat.imageoneurl]; nsurl *url = [nsurl urlwithstring:mainstring]; // nslog(@"%@", mainstring); [cell.mainimageview setimagewithurl:url placeholderimage:nil]; return cell; }
import "uiimage + afnetworking.h"
inside - (uicollectionviewcell *)collectionview:(uicollectionview *)collectionview cellforitematindexpath:(nsindexpath *)indexpath
nsurlrequest *req = [nsurlrequest requestwithurl:[nsurl urlwithstring:cat.imageoneurl]]; [edit] : __weak categorycollectionviewcell *weakcell = cell; [edit] [cell.articlethumbnail setimagewithurlrequest:req placeholderimage:[uiimage imagenamed:@"placeholder.png"] success:^(nsurlrequest *request, nshttpurlresponse *response, uiimage *image) { [edit] weakcell.articlethumbnail.image = image; } failure:^(nsurlrequest *request, nshttpurlresponse *response, nserror *error) { [edit] weakcell.articlethumbnail.image = [uiimage imagenamed:@"placeholder.png"]; }]; hope helps
Comments
Post a Comment