ios - Saving and fetching Multiple Image Selection From Library in Core Data -


i building project using elcimagepickercontroller multiple selection , saving selected image core data. fetching images in vc , showing them uicollectionview.

but problem facing here images not showing in collectionview. having 2 confusions.

  • the images saving core data or not.
  • if images saved, fetching or not.

here entering selected images core data

- (void)imagepicker:(snimagepickernc *)imagepicker didfinishpickingwithmediainfo:(nsmutablearray *)info {      _arrimages = [[nsmutablearray alloc]init];     _imgdata = [[nsmutabledata alloc]init];     appdelegate* app=(appdelegate*)[[uiapplication sharedapplication]delegate];     nsmanagedobjectcontext *context = app.managedobjectcontext;      albums *objphotos = (albums *)[nsentitydescription insertnewobjectforentityforname:@"albums" inmanagedobjectcontext:context];  //get images     (int = 0; < info.count; i++) {         alassetslibrary *assetlibrary=[[alassetslibrary alloc] init];         [assetlibrary assetforurl:info[i] resultblock:^(alasset *asset) {         uiimage *image = [uiimage imagewithcgimage:[asset aspectratiothumbnail]];          _imagedata = [nsdata datawithdata:uiimagepngrepresentation(image)];         [objphotos setphotosinalbum:self.imagedata];        } failureblock:^(nserror *error) {     }]; }  nserror * err = nil; [context save:&err];   if (![context save:&err]) {     nslog(@"can't save! %@ %@", err, [err localizeddescription]); } uialertview *alert = [[uialertview alloc]initwithtitle:@"success!!" message:@"photos selected!" delegate:self cancelbuttontitle:@"done" otherbuttontitles:nil]; [alert show];  }  

here fetching selected images

-(nsarray *)getmencategorylist {     appdelegate* app=(appdelegate*)[[uiapplication sharedapplication]delegate];     nsmanagedobjectcontext *moc=app.managedobjectcontext;     nsfetchrequest *fetch = [[nsfetchrequest alloc] init];      nsentitydescription *entity =  [nsentitydescription entityforname:@"albums" inmanagedobjectcontext:moc];     [fetch setentity:entity];     nserror *error;     nsarray *result = [moc executefetchrequest:fetch error:&error];     if (!result) {         return nil;     }     return result; } 

i calling fetching function in - (nsinteger) collectionview:(uicollectionview *)collectionview numberofitemsinsection:(nsinteger)section , returning array count

- (nsinteger) collectionview:(uicollectionview *)collectionview numberofitemsinsection:(nsinteger)section {      return [[self getmencategorylist] count]; } 

and lastly, populating images collectionview here

- (uicollectionviewcell *) collectionview:(uicollectionview *)collectionview cellforitematindexpath:(nsindexpath *)indexpath {      uicollectionviewcell *cell = (uicollectionviewcell *)[collectionview dequeuereusablecellwithreuseidentifier:@"albumcollectionviewcellcell" forindexpath:indexpath];        photos *objalbumphotos = (photos *)[[self getmencategorylist] objectatindex:indexpath.row];      uiimage *albumimg = [uiimage imagewithdata:[objalbumphotos valueforkey:@"photosinalbum"]];     uiimageview *photosimageview = (uiimageview *)[cell.contentview viewwithtag:1];     photosimageview.image = albumimg;        return cell; } 

any highly appreciated.

note: edit code please confirm first per code , edit per requirement

step 1: generate unique file name , save image file name file path.
step 2: after write file save path core data.
step 3: fetching use core data path images directory.

- (nsstring*)generatefilenamewithextension:(nsstring *)extensionstring {         // extenstion string @".png"          nsdate *time = [nsdate date];         nsdateformatter* df = [nsdateformatter new];         [df setdateformat:@"dd-mm-yyyy-hh-mm-ss"];         nsstring *timestring = [df stringfromdate:time];         int r = arc4random() % 100;         int d = arc4random() % 100;         nsstring *filename = [nsstring stringwithformat:@"file-%@%d%d%@", timestring, r , d , extensionstring ];          nslog(@"file name %@", filename);          return filename;  }     - (void)imagepicker:(snimagepickernc *)imagepicker didfinishpickingwithmediainfo:(nsmutablearray *)info     {          _arrimages = [[nsmutablearray alloc]init];         _imgdata = [[nsmutabledata alloc]init];         appdelegate* app=(appdelegate*)[[uiapplication sharedapplication]delegate];         nsmanagedobjectcontext *context = app.managedobjectcontext;          albums *objphotos = (albums *)[nsentitydescription insertnewobjectforentityforname:@"albums" inmanagedobjectcontext:context];      //get images         (int = 0; < info.count; i++) {             alassetslibrary *assetlibrary=[[alassetslibrary alloc] init];             [assetlibrary assetforurl:info[i] resultblock:^(alasset *asset) {             uiimage *image = [uiimage imagewithcgimage:[asset aspectratiothumbnail]];              _imagedata = [nsdata datawithdata:uiimagepngrepresentation(image)];      nsstring *strfilename = [self generatefilenamewithextension:@".jpg"];       nsarray *paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes);     nsstring *documentsdirectory = [paths objectatindex:0];     nsstring *filepath =  [documentsdirectory stringbyappendingpathcomponent:strfilename];              [_imagedata writetofile:filepath atomically:yes];             [objphotos setphotosinalbum:filepath];          } failureblock:^(nserror *error) {     }];     }      nserror * err = nil;     [context save:&err];       if (![context save:&err]) {         nslog(@"can't save! %@ %@", err, [err localizeddescription]);     }     uialertview *alert = [[uialertview alloc]initwithtitle:@"success!!" message:@"photos selected!" delegate:self cancelbuttontitle:@"done" otherbuttontitles:nil];     [alert show];      }  

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -