//
// ViewController.m
// SelectMorePhoto
//
// Created by phc on 16/7/17.
// Copyright ? 2016年 phc. All rights reserved.
//
#import "ViewController.h"
#import <AssetsLibrary/AssetsLibrary.h>
#import <Photos/Photos.h>
@interface ViewController ()<UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property(nonatomic,strong)NSMutableArray *imgArr;
@property (strong, nonatomic) UICollectionView *collectionView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_imgArr = [NSMutableArray array];
//1.創(chuàng)建資源庫對象(資源庫中包含所有的視頻和照片)
ALAssetsLibrary * library = [[ALAssetsLibrary alloc] init];
//遍歷資源庫
[library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
//ALAssetsGroup代表資源庫中的一個相冊
if (group) {//如果存在,在遍歷
//遍歷相冊中的所有的資源(包括照片,和視頻)
[group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
/*
if (index == 3) {
*stop = YES;
}
*/
//ALAsset代表一個照片
//獲取圖片的縮略圖
CGImageRef cimg = [result thumbnail];
UIImage *img = [UIImage imageWithCGImage:cimg];
//容錯
if (img) {
[_imgArr addObject:img];
}
}];
}
[_collectionView reloadData];
} failureBlock:^(NSError *error) {
NSLog(@"訪問失敗");
}];
[self creactCollection];
}
- (void)creactCollection{
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
//設置單元格尺寸
flowLayout.itemSize = CGSizeMake(70, 70);
_collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
_collectionView.backgroundColor = [UIColor greenColor];
//設置代理方法和數據源方法
_collectionView.delegate = self;
_collectionView.dataSource = self;
[self.view addSubview:_collectionView];
[_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return _imgArr.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
UIImageView *imgView = [[UIImageView alloc] initWithFrame:cell.bounds];
imgView.backgroundColor = [UIColor redColor];
[cell.contentView addSubview:imgView];
imgView.image = _imgArr[indexPath.row];
return cell;
}
@end
選中系統(tǒng)照片庫中多張照片的方法
最后編輯于 :
?著作權歸作者所有,轉載或內容合作請聯系作者
- 文/潘曉璐 我一進店門谐宙,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人界弧,你說我怎么就攤上這事凡蜻。” “怎么了垢箕?”我有些...
- 正文 為了忘掉前任帅掘,我火速辦了婚禮委煤,結果婚禮上,老公的妹妹穿的比我還像新娘修档。我一直安慰自己碧绞,他們只是感情好,可當我...
- 文/花漫 我一把揭開白布吱窝。 她就那樣靜靜地躺著讥邻,像睡著了一般。 火紅的嫁衣襯著肌膚如雪院峡。 梳的紋絲不亂的頭發(fā)上兴使,一...
- 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼赏淌!你這毒婦竟也來了踩寇?” 一聲冷哼從身側響起,我...
- 正文 年R本政府宣布轨帜,位于F島的核電站,受9級特大地震影響衩椒,放射性物質發(fā)生泄漏蚌父。R本人自食惡果不足惜,卻給世界環(huán)境...
- 文/蒙蒙 一烟具、第九天 我趴在偏房一處隱蔽的房頂上張望梢什。 院中可真熱鬧,春花似錦朝聋、人聲如沸。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至狸演,卻和暖如春言蛇,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背宵距。 一陣腳步聲響...
推薦閱讀更多精彩內容
- (一)全面性 通過就業(yè)質量的定義可以看出娇妓,就業(yè)質量是一個多維度的概念,它包括個人在就業(yè)過程的每一個環(huán)節(jié)上被公平對待...
- 城闕輔三秦活鹰,風煙望五津哈恰。 與君離別意,同是宦游人志群。 海內存知己着绷,天涯若比鄰。 無為在歧路锌云,兒女共沾巾蓬戚。 初唐四杰:...