I want to copy picture from the photo library to another directory in my app, every thing works just fine with the attached code here but when I try to copy a lot of images the app just crashes immediately I think that because the attached code is done with a single thread so each image needs its thread, so if there is too many pictures to copy the app crashes. I need the same code here but not in a thread that means the app should be blocked until the image is copied to the another directory , if any one have another good idea I would be appreciated. the for loop is saving each image.
for (int i = 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 is NSData may be what you 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 the 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 the file name
NSString *filePath = [dataPath stringByAppendingPathComponent:Bnamee]; //Add the file name
NSString *filePathS = [dataPath stringByAppendingPathComponent:Snamee]; //Add the file name
[JpgData writeToFile:filePath atomically:YES]; //Write the file
[JpgDataS writeToFile:filePathS atomically:YES]; //Write the file
//[pngData writeToFile:filePath atomically:YES]; //Write the file
return true;
}
thanks in advance