从照片库选择多张图片到collectionView

未经允许,请勿转载

需要用到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];

    


    

    

}


//添加1collectionview

- (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




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值