Lately, we had the following error:
*** -[NSKeyedUnarchiver initForReadingWithData:]: data is NULL
What had we done to get this error?
It turned out, that the reason was a loop in which multiple calls of
UIImageWriteToSavedPhotosAlbum ([assets objectAtIndex:currentIndex], self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
had been sitting. We checked the documentation – not a single mention, that multiple calls could be a problem. On the other hand – that it is asynchronous, that was clear through the callback. And thus we decided to “serialize” our problem, as can be seen below. Problem gone.
Explanation: The action starts with saveButtonPressed, when the call to UIImageWriteToSavedPhotosAlbum is ready it sends a image:didFinishSavingWithError:contextInfo: method-message to the given class and in that method we trigger the next write, if there still is one…
Have a look yourself:
- (void) image: (UIImage image didFinishSavingWithError: (NSError error contextInfo: (void contextInfo {
NSLog(@"didFinishSavingWithError");
if (error != NULL) {
NSLog(@"error");
}
currentIndex++;
if (currentIndex < [assets count]) {
UIImageWriteToSavedPhotosAlbum ([assets objectAtIndex:currentIndex], self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
NSLog(@"currentIndex %i",currentIndex);
}
}
-(IBAction)saveButtonPressed:(id)sender{
NSLog(@"saveButtonPressed");
currentIndex = 0;
UIImageWriteToSavedPhotosAlbum ([assets objectAtIndex:currentIndex], self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
NSLog(@"photos saved");
}