未经允许,请勿转载
需要用到ZYQAssetPickerController.h
//
// ViewController.m
// demo7UICollectionView
//
// Created by wusiping on 15/11/25.
// Copyright (c) 2015年 wusiping. All rights reserved.
//
#import "ViewController.h"
#import "ZYQAssetPickerController.h"
@interface ViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,UINavigationControllerDelegate>
@property(nonatomic, weak)UICollectionView *photoView;
@property(nonatomic, weak)UIButton *btn;
@property (strong, nonatomic) NSMutableArray * imageArray;
@property (nonatomic, retain) NSMutableArray * thumbnailImageArray; //缩略图数组
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self addCollectionView];
[self addButtonAdd];
}
//添加一个按钮,用来打开图片库
- (void)addButtonAdd
{
CGFloat buttonX = [UIScreen mainScreen].bounds.size.width * 0.2 ;
CGFloat buttonY = [UIScreen mainScreen].bounds.size.height * 0.8;
CGFloat buttonW = 200;
CGFloat buttonH = 100;
CGRect buttonFrame = CGRectMake(buttonX, buttonY, buttonW, buttonH);
UIButton *btn = [[UIButton alloc]initWithFrame:buttonFrame];
[btn setTitle:@"添加一张新图片" forState:UIControlStateNormal];
btn.titleLabel.textColor = [UIColor blackColor];
btn.backgroundColor = [UIColor grayColor];
[btn addTarget:self action:@selector(touchBtn:) forControlEvents:UIControlEventTouchUpInside];
_btn = btn;
[self.view addSubview:_btn];
}
//点击按钮后,创建ZYQPiker
- (void)touchBtn:(UIButton *)btn
{
NSLog(@"点击了按钮");
ZYQAssetPickerController *picker = [[ZYQAssetPickerController alloc] init];
picker.maximumNumberOfSelection = 10;
picker.assetsFilter = [ALAssetsFilter allPhotos];
picker.showEmptyGroups=NO;
picker.delegate=self;
picker.selectionFilter = [NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {
if ([[(ALAsset*)evaluatedObject valueForProperty:ALAssetPropertyType] isEqual:ALAssetTypeVideo]) {
NSTimeInterval duration = [[(ALAsset*)evaluatedObject valueForProperty:ALAssetPropertyDuration] doubleValue];
return duration >= 5;
} else {
return YES;
}
}];
[self presentViewController:picker animated:YES completion:NULL];
}
//添加1个collectionview
- (void)addCollectionView
{
CGFloat photoViewX = 0;
CGFloat photoViewY = 0;
CGFloat photoViewW = [UIScreen mainScreen].bounds.size.width;
CGFloat photoViewH = [UIScreen mainScreen].bounds.size.height * 0.5;
CGRect photoViewFrame = CGRectMake(photoViewX, photoViewY, photoViewW, photoViewH);
UICollectionViewFlowLayout *myLayout = [[UICollectionViewFlowLayout alloc]init];
[myLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
UICollectionView *photoView = [[UICollectionView alloc]initWithFrame:photoViewFrame collectionViewLayout:myLayout];
_photoView = photoView;
_photoView.dataSource = self;
_photoView.delegate = self;
[_photoView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"UICollectionViewCell"];
[self.view addSubview:_photoView];
}
#pragma mark - ZYQAssetPickerController Delegate
//将piker选择的图片存到动态数组当中,然后重新加载colletionView
-(void)assetPickerController:(ZYQAssetPickerController *)picker didFinishPickingAssets:(NSArray *)assets{
NSMutableArray * thumbnailImageArray = [[NSMutableArray alloc]init];
NSMutableArray * imageArray = [[NSMutableArray alloc]init];
for (int i=0; i< assets.count; i++) {
ALAsset * asset= assets[i];
UIImage * tempImg = [UIImage imageWithCGImage:asset.defaultRepresentation.fullScreenImage];
//添加未压缩的图片
[imageArray addObject:tempImg];
[thumbnailImageArray addObject:tempImg];
dispatch_async(dispatch_get_main_queue(), ^{
[_photoView reloadData];
NSLog(@"图片添加成功%@",tempImg);
});
asset = nil;
}
_imageArray = imageArray;
_thumbnailImageArray = thumbnailImageArray;
}
#pragma cellectionView的数据源方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 3;
}
//将动态数组里面的图片赋值给cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * CellIdentifier = @"UICollectionViewCell";
UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
NSLog(@"创建cell");
if ( _thumbnailImageArray[indexPath.row] != nil) {
if ([_thumbnailImageArray[indexPath.row] isKindOfClass:[UIImage class]] ) {
UIImageView *cellView = [[UIImageView alloc]init];
cellView.image = _thumbnailImageArray[indexPath.row];
[cell setBackgroundView:cellView];
}
}else{
NSLog(@"没那么多图片");
}
return cell;
}
@end