I'm trying to get last picture from iphone camera roll. I use the following code:
UIImage* __block image = [[UIImage alloc] init];
ALAssetsLibrary* library = [[ALAssetsLibrary alloc] init];
[library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup* group, BOOL* stop) {
[group setAssetsFilter:[ALAssetsFilter allPhotos]];
if ([group numberOfAssets] > 0) {
[group enumerateAssetsAtIndexes:[NSIndexSet indexSetWithIndex:[group numberOfAssets]-1] options:NSEnumerationConcurrent usingBlock:^(ALAsset* alAsset, NSUInteger index, BOOL* innerStop) {
if (alAsset) {
ALAssetRepresentation* rawImage = [alAsset defaultRepresentation];
image = [UIImage imageWithCGImage:[rawImage fullScreenImage]];
[self doTheJobWithImage:image];
// Inside doTheJobWithImage: a segue is also performed at the end
}
}];
}
} failureBlock:^(NSError* error) {
NSLog(@"Error: %@", [error localizedDescription]);
}];
It works but with flaws (I'm using Instruments and Zombies to debug it):
- It seems to read every picture inside camera roll. So, first question: doesn't enumerateAssetsAtIndexes: only retrieve the specified images (atIndexes)? What's wrong with my code?
- Adding to previous problem, memory becomes a problem when a lot of pictures are in camera roll. My app crashes if too many or, if it works, it seems retrieved images are NEVER deallocated, so the second or third time i call this code, it crashes anyway due to memory leaks. So second question: how do I force my code to deallocate everything after calling doTheJobWithImage: ?
I'm using xcode 4.6, ios 6 and ARC. Thanks in advance.