I'm trying to get the most recent video taken on an iOS device as AVAsset so that I can then proceed to do other things with it. I am using this code to try and do it:
However, when running the app on an iPad, I get a SIGABRT error at this line in main:
and the following error in the debugger:
Terminating app due to uncaught exception 'NSRangeException', reason: '* -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array' * First throw call stack: (0x37bc288f 0x32ef3259 0x37b0b9db 0x672af 0x309d4c8b 0x309df267 0x309df1d5 0x30a7e59b 0x30a7d367 0x30ad86a7 0x62351 0x36de860d 0x37b96a33 0x37b96699 0x37b9526f 0x37b184a5 0x37b1836d 0x35ffb439 0x309c9cd5 0x61f35 0x61ef4) terminate called throwing an exception
It seems like it's just skipping over the part where it's supposed to get the asset and then when I try to use the asset later the array is empty so it errors, but I'm not entirely sure. The only information I've really been able to find is related to the lifetime of library objects only lasting as long as the library, but I release the library much later after I'm already done with it so I don't think that's the problem. Does anybody have any ideas? I'm at a complete loss here.
Code:
NSMutableArray *assets = [[NSMutableArray alloc] init];
library = [[ALAssetsLibrary alloc] init];
// Enumerate just the photos and videos group by using ALAssetsGroupSavedPhotos.
[library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
[group setAssetsFilter:[ALAssetsFilter allVideos]];
[group enumerateAssetsAtIndexes:[NSIndexSet indexSetWithIndex:[group numberOfAssets]-1]
options:0
usingBlock:^(ALAsset *alAsset, NSUInteger index, BOOL *innerStop) {
if (alAsset) {
ALAssetRepresentation *representation = [alAsset defaultRepresentation];
NSURL *url = [representation url];
AVAsset *recentVideo = [AVURLAsset URLAssetWithURL:url options:nil];
[assets addObject:recentVideo];
}
}];
}
failureBlock: ^(NSError *error) {
// better error handling needed
NSLog(@"error");
}];
However, when running the app on an iPad, I get a SIGABRT error at this line in main:
Code:
int retVal = UIApplicationMain(argc, argv, nil, nil);
and the following error in the debugger:
Terminating app due to uncaught exception 'NSRangeException', reason: '* -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array' * First throw call stack: (0x37bc288f 0x32ef3259 0x37b0b9db 0x672af 0x309d4c8b 0x309df267 0x309df1d5 0x30a7e59b 0x30a7d367 0x30ad86a7 0x62351 0x36de860d 0x37b96a33 0x37b96699 0x37b9526f 0x37b184a5 0x37b1836d 0x35ffb439 0x309c9cd5 0x61f35 0x61ef4) terminate called throwing an exception
It seems like it's just skipping over the part where it's supposed to get the asset and then when I try to use the asset later the array is empty so it errors, but I'm not entirely sure. The only information I've really been able to find is related to the lifetime of library objects only lasting as long as the library, but I release the library much later after I'm already done with it so I don't think that's the problem. Does anybody have any ideas? I'm at a complete loss here.