问题
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