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

来源:互联网 发布:linux svn客户端 编辑:程序博客网 时间:2024/04/30 03:52

未经允许,请勿转载

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

    [superviewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    [selfaddCollectionView];

    [selfaddButtonAdd];

    

}


//添加一个按钮,用来打开图片库

- (void)addButtonAdd

{

    CGFloat buttonX = [UIScreenmainScreen].bounds.size.width *0.2 ;

    CGFloat buttonY = [UIScreenmainScreen].bounds.size.height *0.8;

    CGFloat buttonW = 200;

    CGFloat buttonH = 100;

    CGRect buttonFrame = CGRectMake(buttonX, buttonY, buttonW, buttonH);

    UIButton *btn = [[UIButtonalloc]initWithFrame:buttonFrame];

    [btn setTitle:@"添加一张新图片"forState:UIControlStateNormal];

    btn.titleLabel.textColor = [UIColorblackColor];

    btn.backgroundColor = [UIColorgrayColor];

    [btn addTarget:selfaction:@selector(touchBtn:)forControlEvents:UIControlEventTouchUpInside];

    _btn = btn;

    [self.viewaddSubview:_btn];

    

}



//点击按钮后,创建ZYQPiker

- (void)touchBtn:(UIButton *)btn

{

    NSLog(@"点击了按钮");

    ZYQAssetPickerController *picker = [[ZYQAssetPickerControlleralloc] init];

    picker.maximumNumberOfSelection =10;

    picker.assetsFilter = [ALAssetsFilterallPhotos];

    picker.showEmptyGroups=NO;

    picker.delegate=self;

    picker.selectionFilter = [NSPredicatepredicateWithBlock:^BOOL(id evaluatedObject,NSDictionary *bindings) {

        if ([[(ALAsset*)evaluatedObjectvalueForProperty:ALAssetPropertyType]isEqual:ALAssetTypeVideo]) {

            NSTimeInterval duration = [[(ALAsset*)evaluatedObjectvalueForProperty:ALAssetPropertyDuration]doubleValue];

            return duration >= 5;

        } else {

            return YES;

        }

    }];

    

    [selfpresentViewController:picker animated:YEScompletion:NULL];

    


    

    

}


//添加1collectionview

- (void)addCollectionView

{

    CGFloat photoViewX = 0;

    CGFloat photoViewY = 0;

    CGFloat photoViewW = [UIScreenmainScreen].bounds.size.width;

    CGFloat photoViewH = [UIScreenmainScreen].bounds.size.height *0.5;

    CGRect photoViewFrame = CGRectMake(photoViewX, photoViewY, photoViewW, photoViewH);

    UICollectionViewFlowLayout *myLayout = [[UICollectionViewFlowLayoutalloc]init];

    [myLayout setScrollDirection:UICollectionViewScrollDirectionVertical];

    UICollectionView *photoView = [[UICollectionViewalloc]initWithFrame:photoViewFramecollectionViewLayout:myLayout];

    _photoView = photoView;

    _photoView.dataSource =self;

    _photoView.delegate =self;

    [_photoViewregisterClass:[UICollectionViewCellclass] forCellWithReuseIdentifier:@"UICollectionViewCell"];

    [self.viewaddSubview:_photoView];

    

}




#pragma mark - ZYQAssetPickerController Delegate

//piker选择的图片存到动态数组当中,然后重新加载colletionView

-(void)assetPickerController:(ZYQAssetPickerController *)picker didFinishPickingAssets:(NSArray *)assets{

    

    NSMutableArray * thumbnailImageArray = [[NSMutableArrayalloc]init];

    NSMutableArray * imageArray = [[NSMutableArrayalloc]init];


    for (int i=0; i< assets.count; i++) {

        ALAsset * asset= assets[i];

        UIImage * tempImg = [UIImageimageWithCGImage: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 = [collectionViewdequeueReusableCellWithReuseIdentifier:CellIdentifierforIndexPath:indexPath];

    NSLog(@"创建cell");

    if ( _thumbnailImageArray[indexPath.row] !=nil) {

        if ([_thumbnailImageArray[indexPath.row]isKindOfClass:[UIImageclass]] ) {

            UIImageView *cellView = [[UIImageViewalloc]init];

            cellView.image = _thumbnailImageArray[indexPath.row];

            

            [cell setBackgroundView:cellView];

        }


    }else{

        NSLog(@"没那么多图片");

    }

        return cell;

}





@end




0 0