ALAssetLibraryGroup 在块中找到相机胶卷

Posted

技术标签:

【中文标题】ALAssetLibraryGroup 在块中找到相机胶卷【英文标题】:ALAssetLibraryGroup find camera roll in block 【发布时间】:2014-04-13 05:47:30 【问题描述】:

self.assetsGroup 为零时,我正在尝试获取 ALAssetsGroup“相机胶卷”。

我的问题是它是异步的,我正在尝试弄清楚如何进行这种同步...如果可能的话。

- (void)viewDidLoad

    [super viewDidLoad];

    if (!self.assetsLibrary) 
        _assetsLibrary = [[ALAssetsLibrary alloc] init];
    

    if (!self.assetsGroups) 
        _assetsGroups = [[NSMutableArray alloc] init];
     else 
        [self.assetsGroups removeAllObjects];
    

    ALAssetsLibraryGroupsEnumerationResultsBlock listGroupBlock = ^(ALAssetsGroup *group, BOOL *stop) 

        ALAssetsFilter *onlyPhotosFilter = [ALAssetsFilter allPhotos];
        [group setAssetsFilter:onlyPhotosFilter];
        if ([group numberOfAssets] > 0)
        
            [self.assetsGroups addObject:group];
         else 
            [self.collectionView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
        
    ;

    // enumerate only photos
    NSUInteger groupTypes = ALAssetsGroupAlbum | ALAssetsGroupEvent | ALAssetsGroupFaces | ALAssetsGroupSavedPhotos;
    [self.assetsLibrary enumerateGroupsWithTypes:groupTypes usingBlock:listGroupBlock failureBlock:nil];


- (void)viewWillAppear:(BOOL)animated

    [super viewWillAppear:animated];

    if (!self.assetsGroup) 
        __block ALAssetsGroup *cameraRollGroup = nil;
        NSUInteger groupTypes = ALAssetsGroupAlbum | ALAssetsGroupEvent | ALAssetsGroupFaces | ALAssetsGroupSavedPhotos;
        [self.assetsLibrary enumerateGroupsWithTypes:groupTypes usingBlock:^(ALAssetsGroup *group, BOOL *stop) 
            // Set to Camera Roll
            if ([[group valueForProperty:@"ALAssetsGroupPropertyType"] intValue] == ALAssetsGroupSavedPhotos) 
                cameraRollGroup = group;
                *stop = YES;
                return;
            
         failureBlock:^(NSError *error) 

        ];

        self.assetsGroup = cameraRollGroup;
    

    self.title = [self.assetsGroup valueForProperty:ALAssetsGroupPropertyName];
    NSLog(@"TITLE %@", self.title); // outputs NULL


    if (!self.assets) 
        _assets = [[NSMutableArray alloc] init];
     else 
        [self.assets removeAllObjects];
    

    ALAssetsGroupEnumerationResultsBlock assetsEnumerationBock = ^(ALAsset *result, NSUInteger index, BOOL *stop) 

        if (result) 
            [self.assets addObject:result];
         else 
            [self.collectionView reloadData];
        

    ;

    ALAssetsFilter *onlyPhotosFilter = [ALAssetsFilter allPhotos];
    [self.assetsGroup setAssetsFilter:onlyPhotosFilter];
    [self.assetsGroup enumerateAssetsUsingBlock:assetsEnumerationBock];

【问题讨论】:

【参考方案1】:

我最终做的是在展示 uiviewcollection 控制器之前加载组。

- (void)presentAlbumCollection

    ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc] init];
    NSUInteger groupTypes = ALAssetsGroupSavedPhotos;
    [assetsLibrary enumerateGroupsWithTypes:groupTypes usingBlock:^(ALAssetsGroup *group, BOOL *stop) 
        if ([[group valueForProperty:@"ALAssetsGroupPropertyType"] intValue] == ALAssetsGroupSavedPhotos) 

            AlbumCollectionViewController *albumCollectionViewController = [[AlbumCollectionViewController alloc] initWithAssetsLibrary:assetsLibrary];
            albumCollectionViewController.assetsGroup = group;
            [self.view.window.rootViewController presentViewController:albumCollectionViewController animated:YES completion:nil];

        
     failureBlock:^(NSError *error) 
        NSLog(@"NOT FOUND!");
    ];

【讨论】:

以上是关于ALAssetLibraryGroup 在块中找到相机胶卷的主要内容,如果未能解决你的问题,请参考以下文章

无法在块中翻译 Django 模板

在块中调用 autorelease

在块中复制和自动释放本地变量

有人可以解释 Ruby 在块中使用管道字符吗?

如何在块中使用 File.ReadAllBytes

在块中调用时未发出 CLLocationManager 身份验证请求