Get Camera Roll photos to a Custom Collection View

限于喜欢 提交于 2020-02-01 05:38:27

问题


I have an Custom Collection View with Cells. Here is my CollectionView code.

- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath;
{
    Cell *cell = [cv dequeueReusableCellWithReuseIdentifier:@"MY_CELL" forIndexPath:indexPath];

    return cell;
}

I want to get images from Camera Roll & display in these cells. How can I get Camera Roll images to view in this Cell? Should I use an array to store names of the images & then show them in the Cell? Can anyone please give me a solution with a code.


回答1:


You can get the Albums and Photo/video assets using <AssetsLibrary/AssetsLibrary.h> framework :

-(void)getAlbum
{
   library = [[ALAssetsLibrary alloc] init];
ALAssetsLibraryGroupsEnumerationResultsBlock listGroupBlock = ^(ALAssetsGroup *group, BOOL *stop) {

        if (group)
       {
            [_albumsArray addObject:group];
        } else {
            [self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
        }
    };

   [library enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:listGroupBlock failureBlock:failureBlock]
}

While selecting album you will get photos/video by below method

-(void)getAssets
{
    ALAssetsGroupEnumerationResultsBlock assetsEnumerationBlock = ^(ALAsset * result, NSUInteger index, BOOL *stop) {

        if (result)
        {

        }
        else
        {
            [self.tableView reloadData];
        }
    };

    [_assetGroup setAssetsFilter:[ALAssetsFilter allAssets]];

    [_assetGroup enumerateAssetsUsingBlock:assetsEnumerationBlock];
}

Please refer this link for more info and example with table view. Alos you can replace table view with your UICollectionView

Thanks!



来源:https://stackoverflow.com/questions/22575568/get-camera-roll-photos-to-a-custom-collection-view

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