iphone - How to remove duplicate data from nsmutableArray -
this question has answer here:
hi iam working iphone photo library. got photos photo library assetframework. displayed photos in scrollview , displaying , images count assume 6. when iam clicking single image, show large image. done. problem "count 12 (double count) when clicking image show large."
i used below code images:
- (void)createscrollview { @try { nslog(@"in create scrollview"); //add views scrolview // uiimageview *backgroundimgview; int x=5; int y=7; nslog(@"assetsarray/count/createscrollview %d",assetsarray.count); (int i=0;i<[assetsarray count];i++) { uiview *userview=[[uiview alloc] initwithframe:cgrectmake(x, y, 70, 80)]; userview.tag=i; uiimageview *backgroundimgview=[[uiimageview alloc] initwithframe:cgrectmake(1, 1, 70, 70)]; backgroundimgview.tag=1; // [backgroundimgview setimagewithurl:[nsurl urlwithstring:[urlstring stringbyaddingpercentescapesusingencoding:nsutf16bigendianstringencoding]] placeholderimage:[uiimage imagenamed:@"noimage.png"]]; //-------------getting images assetslibrary ---------- alassetslibraryassetforurlresultblock resultblock = ^(alasset *myasset) { galleryobj=[[galleryobject alloc]init]; alassetrepresentation *rep = [myasset defaultrepresentation]; cgimageref iref = [rep fullresolutionimage]; uiimage *assetslibraryimage; if (iref) { assetslibraryimage = [uiimage imagewithcgimage:iref scale:[rep scale] orientation:(uiimageorientation)[rep orientation]]; galleryobj.galleryimage=assetslibraryimage; } else { assetslibraryimage = [uiimage imagenamed:@"noimage.png"]; } //[set addobject:[nsstring stringwithformat:@"1"]]; [uniqueset addobject:galleryobj]; nslog(@"uniqueset data .....%@",uniqueset); // output (3,1,4,2,5) ... objects [imagesarray addobject:galleryobj]; nslog(@"imagesarray/resultblock count %d array %@....",imagesarray.count,imagesarray); backgroundimgview.image=assetslibraryimage; }; alasset *al_asset = [assetsarray objectatindex:i]; //nslog(@"al_asset ......%@",al_asset); al_asseturl=al_asset.defaultrepresentation.url; //nslog(@"al_asseturl %@",al_asseturl); alassetslibraryaccessfailureblock failureblock = ^(nserror *myerror) { nslog(@"alassetslibraryaccessfailureblock"); }; alassetslibrary* assetslibrary = [[alassetslibrary alloc] init]; [assetslibrary assetforurl:al_asseturl resultblock:resultblock failureblock:failureblock]; //-------------getting images assetslibrary ---------- uibutton *userbutton=[[uibutton alloc]initwithframe:cgrectmake(1, 1, 70,70)]; [userbutton addtarget:self action:@selector(userimageclicked:) forcontrolevents:uicontroleventtouchupinside]; userbutton.tag=i; [userview addsubview:backgroundimgview]; [userview addsubview:userbutton]; [self.galleryscrollview addsubview:userview]; x+=79; if ((i+1)%4==0) { //if added image 4th image y+=80; x=5; } // [activity stopanimating]; } if (y+100>self.galleryscrollview.frame.size.height) { self.galleryscrollview.contentsize=cgsizemake(320, y+100); } else { self.galleryscrollview.contentsize=cgsizemake(320, self.galleryscrollview.frame.size.height+60); } } @catch (nsexception *exception) { nslog(@"exception %@",exception); } } please notice created button , action userimageclicked in above method. when iam clicking userimageclicked button, array count double.
i dont know why happened. try remove duplicates using containsobject method. no use.
in above method, saved uiimage in objectclass , assinging object imagesarray.
i took nsmutableset store value, no use.
please 1 can suggest solve issue.
this how delete duplicate data:
nsarray *copy = [mutablearray copy]; nsinteger index = [copy count] - 1; (id object in [copy reverseobjectenumerator]) { if ([mutablearray indexofobject:object inrange:nsmakerange(0, index)] != nsnotfound) { [mutablearray removeobjectatindex:index]; } index--; } please use accordingly...
a simpler way:
nsmutablearray *unique = [nsmutablearray array]; (id obj in originalarray) { if (![unique containsobject:obj]) { [unique addobject:obj]; } }
Comments
Post a Comment