I wrote a simple iOS program to get the number of photos that are saved in the camera frame using the Assets framework provided in SDK4.2.
The program worked well as I expected when I ran it on an iPhone simulator. But he didn’t get any images when I launched the “real” iPhone device (iPhone 3GS with iOS 4.2.1).
This problem looks the same as the problem discussed in the following article:
The structure of the resource library is not working correctly 4.0 and 4.2
So, I added the dispatch_async (dispatch_get_main_queue () ... "function, as shown below, but I could not solve the problem.
- (void)viewDidLoad {
[super viewDidLoad];
NSMutableArray assets = [[NSMutableArray array] retain];
void (^assetEnumerator)(struct ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *asset, NSUInteger index, BOOL *stop) {
if(asset != NULL) {
[assets addObject:asset];
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"%i", [assets count]);
});
}
};
void (^assetGroupEnumerator)(struct ALAssetsGroup *, BOOL *) = ^(ALAssetsGroup *group, BOOL *stop) {
if(group != nil) {
[group enumerateAssetsUsingBlock:assetEnumerator];
}
};
ALAssetsLibrary* library = [[ALAssetsLibrary alloc] init];
[library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos
usingBlock:assetGroupEnumerator
failureBlock: ^(NSError *error) {
NSLog(@"Failed.");
}];
}
, - ?