ios - Copy UImages from photo library to another directory -


i want copy picture photo library directory in app, every thing works fine attached code here when try copy lot of images app crashes think because attached code done single thread each image needs thread, if there many pictures copy app crashes. need same code here not in thread means app should blocked until image copied directory , if 1 have idea appreciated. loop saving each image.

for (int = 0; i< countt ;i++) {          nsurl *referenceurl = [self.tosavearray objectatindex:i];         alassetslibrary *assetlibrary=[[alassetslibrary alloc] init];         [assetlibrary assetforurl:referenceurl resultblock:^(alasset *asset) {             alassetrepresentation *rep = [asset defaultrepresentation];             byte *buffer = (byte*)malloc(rep.size);             nsuinteger buffered = [rep getbytes:buffer fromoffset:0.0 length:rep.size error:nil];             nsdata *data = [nsdata datawithbytesnocopy:buffer length:buffered freewhendone:yes];//this nsdata may want             nslog(@"length %d",[data length]);             uiimage *image= [uiimage imagewithdata:data];             [self savephoto:image withnum:i];             //[data writetofile:photofile atomically:yes];//you can save image later         } failureblock:^(nserror *err) {             nslog(@"error: %@",[err localizeddescription]);         }];      } 

save photo code:

-(bool)savephoto:(uiimage *) imagetosave withnum:(int)num{       float goodscal = imagetosave.size.width/75.0;      cgsize newsize =cgsizemake(imagetosave.size.width/goodscal, imagetosave.size.height/goodscal);     uiimage* smallimage = [self resizeimage:imagetosave tosize:newsize];     nsdata *jpgdatas = uiimagejpegrepresentation(smallimage, 1);     nsdata *jpgdata = uiimagejpegrepresentation(imagetosave, 1);        nsarray *paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes);     nsstring *documentspath = [paths objectatindex:0]; //get docs directory     nsstring *datapath = [documentspath stringbyappendingpathcomponent:@"/capturesphotos"];     nsdate *date = [nsdate date];     nsdateformatter *formatter = [[[nsdateformatter alloc] init]autorelease];     nstimezone *zone = [nstimezone localtimezone];     [formatter settimezone:zone];     [formatter setdateformat:@"yyyymmddhhmmss"];     nsstring* bnamee = [nsstring stringwithformat:@"/img%d_%@b.jpg",num,[formatter stringfromdate:date]];     nsstring* snamee = [nsstring stringwithformat:@"/img%d_%@s.jpg",num,[formatter stringfromdate:date]];     //nsstring *filepath = [datapath stringbyappendingpathcomponent:namee]; //add file name     nsstring *filepath = [datapath stringbyappendingpathcomponent:bnamee]; //add file name     nsstring *filepaths = [datapath stringbyappendingpathcomponent:snamee]; //add file name     [jpgdata writetofile:filepath atomically:yes]; //write file     [jpgdatas writetofile:filepaths atomically:yes]; //write file     //[pngdata writetofile:filepath atomically:yes]; //write file     return true; } 

thanks in advance

it seems starting off many asynchronous operations running out of memory, try serializing expensive (memory-wise) parts of using serial queue:

dispatch_queue_t    queue = dispatch_queue_create("save", dispatch_queue_serial);  // move out of loop efficiency alassetslibrary*    assetlibrary=[[alassetslibrary alloc] init];  (int = 0; i< tosavearray.count ;i++) {      nsurl *referenceurl = [tosavearray objectatindex:i];     [assetlibrary assetforurl:referenceurl resultblock:^(alasset *asset) {         alassetrepresentation *rep = [asset defaultrepresentation];         dispatch_async(queue, ^{              byte *buffer = (byte*)malloc(rep.size);             nsuinteger buffered = [rep getbytes:buffer fromoffset:0.0 length:rep.size error:nil];             nsdata *data = [nsdata datawithbytesnocopy:buffer length:buffered freewhendone:yes];//this nsdata may want             nslog(@"length %d",[data length]);             uiimage *image= [uiimage imagewithdata:data];             [self savephoto:image withnum:i];         });         //[data writetofile:photofile atomically:yes];//you can save image later     } failureblock:^(nserror *err) {         nslog(@"error: %@",[err localizeddescription]);     }];  }  dispatch_async(queue, ^{     // code execute when saving done }); 

i'm not sure if solve problem, it's worth trying :)

also, corrected version of how uiimage more cleanly (from this answer):

uiimage*            image = [uiimage imagewithcgimage:[rep fullresolutionimage]                                                 scale:[rep scale]                                           orientation:uiimageorientationup]; 

although in case it's better continue fetching image data itself, pass save routine can save expense of converting jpg->image->jpg. still need create image can save thumbnail, that's matter.


Comments

Popular posts from this blog

java - Intellij Synchronizing output directories .. -

git - Initial Commit: "fatal: could not create leading directories of ..." -