ALAssetsLibrary returns same assets after adding new photo to camera roll - objective c

强颜欢笑 提交于 2019-12-21 06:28:09

问题


I'm using ALAssetsLibrary to enumerate first 30 photos in my Saved photos. I've created property in AppDelegate to have access to ALAssets everytime when I want, because lifetime of ALAssetsLibrary instance is tied to ALAssets lifetime. Here is my code:

if(!_savedAssets) _savedAssets = [[NSMutableArray alloc] init];
else [_savedAssets removeAllObjects];

if(appDelegate.library)
{
    [appDelegate.library release];
    [appDelegate setLibrary:nil];
}

appDelegate.library = [ALAssetsLibrary new];
__block int count = 0;
[appDelegate.library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
    NSLog(@"%d",group.numberOfAssets);
    if(count>30) *stop = YES;
    [group enumerateAssetsWithOptions:NSEnumerationReverse usingBlock:^(ALAsset *result, NSUInteger index, BOOL *needToStop) {
        NSLog(@"%d", index);
        if(count<30 && result)
        {
            UIImage *image = [UIImage imageWithCGImage:result.thumbnail];
            [_savedAssets addObject:result];
            [self performSelectorOnMainThread:@selector(imageManipulation:) withObject:image waitUntilDone:NO];
            count++;

        }
        else
          *needToStop = YES;

    }];
} failureBlock:^(NSError *error) {
    NSLog(@"%@",error.description);
}];

As you can see, I'm adding first 30 ALAssets to my NSMutableArray, to have access to thumbnail or fullsize image. After my app makes photo, I save this photo to camera roll and try to reload my ALAssets with same code as when I load them first time. Before reloading, I remove all objects from NSMutableArray, and reinitializing ALAssetsLibrary. But this not helps. Each time I get same ALAssets. I get new ALAssets only after reloading app. How to reload ALAssets without reloading app?


回答1:


Register an observer for ALAssetsLibraryChangedNotification. When the notification fires, reload the assets.



来源:https://stackoverflow.com/questions/13038225/alassetslibrary-returns-same-assets-after-adding-new-photo-to-camera-roll-obje

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!