首页 > 代码库 > iOS频繁打开相册崩溃: ALAssetsLibrary error - “Too many contexts. No space in contextList.”

iOS频繁打开相册崩溃: ALAssetsLibrary error - “Too many contexts. No space in contextList.”

iOS频繁打开相册崩溃: ALAssetsLibrary error - “Too many contexts. No space in contextList.”

http://stackoverflow.com/questions/13480611/has-anyone-experienced-crashes-when-using-alassetslibrary-in-a-background-thread

 

原因:

因为频繁的调用  

[[ALAssetsLibrary alloc] init]

即使你相册关闭, 但如果频繁调用这句话, 在ARC方式下, 估计iOS也没有及时清空内存, 

就会导致 “Too many contexts. No space in contextList.”

 

因此, 解决方式就是使用单例调用.

 

+ (ALAssetsLibrary *) defaultAssetsLibrary{    static dispatch_once_t pred = 0;    static ALAssetsLibrary *library = nil;    dispatch_once(&pred, ^{        library = [[ALAssetsLibrary alloc] init];    });    return library;}

 

 或者我用了 DBCamera 库, 它已经提供了一个方法, 直接用也行.

 ALAssetsLibrary *sharedLibrary = [[DBLibraryManager sharedInstance] defaultAssetsLibrary];        [sharedLibrary enumerateGroupsWithTypes:ALAssetsGroupAll                                     usingBlock:^(ALAssetsGroup *group, BOOL *stop)         {             if (group)             {                 NSString *nameStr = [group valueForProperty:ALAssetsGroupPropertyName];                          }                             failureBlock:^(NSError *error)         {             NSLog(@"failureBlock");         }];

  

 

测试后已经不那么容易崩溃了.

 

iOS频繁打开相册崩溃: ALAssetsLibrary error - “Too many contexts. No space in contextList.”