获取本地相册并显示
来源:互联网 发布:centos 安装gitlab 编辑:程序博客网 时间:2024/05/21 10:28
本代码适用于iOS8,iOS9
#import "ViewController.h"
#import "ImageCell.h"
#import <Photos/Photos.h>
#import "UIView+Ext.h"
#define identifier @"image"
@interface ViewController () <UICollectionViewDataSource>
@property (weak, nonatomic) IBOutletUICollectionViewFlowLayout *layout;
@property (weak, nonatomic) IBOutletUICollectionView *collectionView;
@property (nonatomic,strong) NSMutableArray *images;
@end
@implementation ViewController
- (NSMutableArray *)images {
if (_images ==nil) {
_images = [NSMutableArrayarray];
}
return_images;
}
- (void)viewDidLoad {
[superviewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[selfsetupCollectionView];
[selffetchImages];
}
- (void)setupCollectionView {
[self.collectionViewregisterNib:[UINibnibWithNibName:@"ImageCell"bundle:nil]forCellWithReuseIdentifier:identifier];
NSInteger n = 4;
CGFloat margin = 1;
CGFloat width = (ScreenW - margin * (n +1)) / n;
self.layout.itemSize =CGSizeMake(width, width);
self.layout.sectionInset =UIEdgeInsetsMake(margin, margin, 0, margin);
}
- (void)fetchImages {
__weak typeof(self) weakSelf =self;
PHImageManager *manager = [[PHImageManageralloc] init];
PHFetchResult *assetCollections = [PHAssetCollectionfetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbumsubtype:PHAssetCollectionSubtypeAnyoptions:nil];
[assetCollections enumerateObjectsUsingBlock:^(PHAssetCollection *assetCollection,NSUInteger idx, BOOL *_Nonnull stop) {
if ([assetCollection.localizedTitleisEqualToString:@"Camera Roll"]) {
weakSelf.title = assetCollection.localizedTitle;
PHFetchResult *assets = [PHAssetfetchKeyAssetsInAssetCollection:assetCollectionoptions:nil];
[assets enumerateObjectsUsingBlock:^(PHAsset *asset,NSUInteger idx, BOOL *_Nonnull stop) {
[manager requestImageForAsset:assettargetSize:self.layout.itemSizecontentMode:PHImageContentModeAspectFilloptions:nilresultHandler:^(UIImage *_Nullable result,NSDictionary *_Nullable info) {
[weakSelf.images addObject:result];
if (idx == assets.count -1) {
[weakSelf.collectionViewreloadData];
}
}];
}];
}
}];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return self.images.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
ImageCell *cell = [collectionViewdequeueReusableCellWithReuseIdentifier:identifierforIndexPath:indexPath];
cell.image = self.images[indexPath.row];
return cell;
}
@end
- 获取本地相册并显示
- 获取本地所有图片显示并可多选的本地相册
- 获取本地图片并显示
- IOS 单击图片全屏显示并存储到本地相册
- android 开启本地相册选择图片并返回显示
- android 开启本地相册选择图片并返回显示
- 获取网络图片并下载到本地相册
- 获取手机相册图片显示并获取路径
- 获取本地相册图片
- 获取本地视频相册
- 获取本地相册图片
- 相机 + 相册中获取相片并裁剪显示
- 从相机or相册获取图片并显示
- android通过拍照、相册获取图片并显示
- 【Android】系统相机、相册获取照片并显示
- 从本地相册获取图片
- 循环获取本地相册图片
- 打开本地相册,获取照片
- The Suspects(并查集问题)
- 给 Android 开发者的 RxJava 详解
- 40.View the Exhibit to examine the description for the SALES and PRODUCTS tables.
- jquery bootgrid的排序功能
- SAS 时区管理 -2
- 获取本地相册并显示
- SCOI2005[互不侵犯KING]
- 34.App页面中数据加载的6种方式
- python实现基础排序算法之(冒泡排序)
- Android中View的弹性滑动——Android开发艺术探索笔记
- Linux第一天笔记2016年01月08日(周五)
- Android学习笔记day5
- 够您学习一辈子的生活经典
- MQTT 测试工具