Closed sahil263 closed 9 years ago
@sahil263 u can try to implement a method to invoke the image saving logic, then in completion/failure block, invoke this method, and offer a counter to stop the loop when needed.
There's a similar issue discussed here: https://github.com/Kjuly/ALAssetsLibrary-CustomPhotoAlbum/issues/24. Which request the image from server, then download/save it one by one. Hope it helps. :)
@Kjuly Thanks! Yes, I end up using the following method. Here it is, for others:
@interface YOUR_VC () {
__block NSError *errorWhileExporting ;
int imagesSavedCount;
}
@end
@implementation YOUR_VC
-(void)saveButtonPresses {
imagesSavedCount = 0;
[self saveLogImagesToAlbum:@"ALBUM_NAME" startFromIndex:0];
}
-(void)saveLogImagesToAlbum:(NSString *)albumNameTo startFromIndex:(int)i {
if (i >= UIIMAGE_ARRAY.count) {
[self exportImagesToAlbumCompleted:errorWhileExporting];
}
else {
[self.assetsLibrary saveImage:[UIIMAGE_ARRAY objectAtIndex:i]
toAlbum: albumNameTo
completion:^(NSURL *assetURL, NSError *error) {
if (error) {
errorWhileExporting = error;
// Image NOT saved, call method again for saving next image.
} else {
imagesSavedCount++;
// Image saved, call method again for saving next image.
}
[self saveLogImagesToAlbum:albumNameTo startFromIndex:i+1];
}
failure:^(NSError *error) {
if (error) {
errorWhileExporting = error;
}
// Image NOT saved, call method again for saving next image.
[self saveLogImagesToAlbum:albumNameTo startFromIndex:i+1];
}];
}
}
- (void)exportImagesToAlbumCompleted:(NSError *)error {
if (error && imagesSavedCount==0) {
NSLog(@"%@", [@"Oops!\n" stringByAppendingString:[error localizedDescription]]);
} else {
NSLog(@"%d/%d Images saved to camera-roll album.", imagesSavedCount, UIIMAGE_ARRAY.count);
// When some images fail.
NSLog(@"Optional Error %@", [error localizedDescription]);
}
}
@end
I am using the code mentioned in issue #2. But, when I try to save 17 images or so it works fine for some images and the process fails and gives following error:
_block_invoke238: Failed to add the asset to the custom photo album: Write busy
and only some images get save to the album. The problem is that the
for loop
is not waiting for the image to save or fail and it's firing multiple save requests... which overloads thewriteImageToSavedPhotosAlbum
method.