从照片库选择多张图片到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];
}
//添加1个collectionview
- (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
- 从照片库选择多张图片到collectionView
- 从系统相册选择多张图片
- 从系统相册选择多张图片
- 从系统相册选择多张图片
- 从系统相册选择多张图片
- 从系统相册选择多张图片
- 从相册中选择多张图片
- 多张图片选择
- ios 从系统相册选择多张图片
- Swift-->从相册(摄像头)选择照片,图片选择
- 相机选择照片及拍照后使用AFNetworking图片上传(支持一张及多张上传)
- ionic中实现从相册中选择图片并一次上传多张图片
- iOS swift-从照片库中选择照片
- Android从图库选择照片并获取图片的path
- 从相册或拍照选择照片到EditText
- oc照片库图片的选择处理
- android 选择图片拍照并剪切照片上传到服务器
- ionic选择多张图片上传
- Android重点知识(四)ANR异常及解决办法
- css隐藏/显示文字的方法
- TS科普16 节目相关表PAT
- andriod 配置第三方模拟器出现genimotion打不开的情况
- gcc与g++
- 从照片库选择多张图片到collectionView
- canvas基础练习
- hdu 1058 Humble Numbers(优先队列+set)
- 详解Ajax技术
- LaTeX 矩阵
- UTF8、UTF16 之间的转换
- PHP开发工具及配置过程
- final
- IOS中类和对象还有,nil/Nil/NULL的区别