| 本帖最后由 Sian 于 2015-2-26 16:03 编辑 
 1、效果示意图:
 
   
 2、使用方法
 2.1、创建一个导航控制器
 2.2、设置照片所在目录
 2.3、设置接收返回的图片路径
 
 [Objective-C] 纯文本查看 复制代码     NSString *path = [[SACache imageCachesFolder] stringByAppendingPathComponent:@"20150226"];
    SAImagePickViewCtrl *imagePick = [[SAImagePickViewCtrl alloc] initWithFoldPath:path];
    imagePick.resultBlock = ^(NSString *result){
        // 选中照片后执行
    };
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:imagePick];
    [self presentViewController:nav animated:YES completion:nil];
 3、代码参考
 SAImagePickViewCtrl.h
 
 SAImagePickViewCtrl.m[Objective-C] 纯文本查看 复制代码 //
//  SAImagePickViewCtrl.h
//
//  Created by yusian on 15-2-26.
//  Copyright (c) 2015年 Sian. All rights reserved.
//
#import <UIKit/UIKit.h>
typedef void (^SAImagePickViewBlock)(NSString *result);
@interface SAImagePickViewCtrl : UICollectionViewController
@property (nonatomic, copy) SAImagePickViewBlock resultBlock;
- (instancetype)initWithFoldPath:(NSString *)path;
@end
 [Objective-C] 纯文本查看 复制代码 //
//  SAImagePickViewCtrl.m
//
//  Created by yusian on 15-2-26.
//  Copyright (c) 2015年 Sian. All rights reserved.
//
#import "SAImagePickViewCtrl.h"
@interface SAImagePickViewCtrl ()
@property (nonatomic, strong) NSString  *path;
@property (nonatomic, strong) NSArray   *images;
@property (nonatomic, strong) NSArray   *imagePaths;
@end
@implementation SAImagePickViewCtrl
- (instancetype)initWithFoldPath:(NSString *)path
{
    self.title = @"照片选择";
    self.path = path;
    // 1.流水布局
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    // 2.每个cell的尺寸
    CGFloat width = SAScreenSize.width / 4;
    layout.itemSize = CGSizeMake(width - 5, width - 5);
    // 3.设置cell之间的水平间距
    layout.minimumInteritemSpacing = 0;
    // 4.设置cell之间的垂直间距
    layout.minimumLineSpacing = 5;
    // 5.设置四周的内边距
    layout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
    
    return [super initWithCollectionViewLayout:layout];
}
// 文件夹路径
- (void)setPath:(NSString *)path
{
    _path = path;
    NSError *error = nil;
    NSFileManager *fileManager = [[NSFileManager alloc] init];
    self.imagePaths = [fileManager contentsOfDirectoryAtPath:path error:&error];
    if (error) SALog(@"%@", error.localizedDescription);
}
// 文件数组
- (void)setImagePaths:(NSArray *)imagePaths
{
    NSMutableArray *array = [NSMutableArray array];
    for (NSString *string in imagePaths) {
        NSString *imagePath = [self.path stringByAppendingPathComponent:string];
        [array addObject:imagePath];
    }
    self.images = array;
    _imagePaths = array;
}
// 图片数组
- (void)setImages:(NSArray *)images
{
    NSMutableArray *array = [NSMutableArray array];
    for (NSString *path in images) {
        UIImage *image = [UIImage imageWithContentsOfFile:path];
        [array addObject:image];
    }
    _images = array;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"SAImagePickImage"];
    self.collectionView.backgroundColor = SAGrayColor(0.95);
    UIBarButtonItem *cancel = [UIBarButtonItem buttonWithTitle:@"取消" target:self action:@selector(cancel)];
    self.navigationItem.leftBarButtonItem = cancel;
}
// 单元格数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.images.count;
}
// 单元格内容
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SAImagePickImage" forIndexPath:indexPath];
    cell.backgroundView = [[UIImageView alloc] initWithImage:self.images[indexPath.row]];
    return cell;
}
// 点击事件
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.resultBlock){
        self.resultBlock(self.imagePaths[indexPath.row]);
    }
    [self cancel];
    SALog(@"%@", self.imagePaths[indexPath.row]);
}
- (void)cancel
{
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (void)dealloc
{
    SALog(@"SAImagePickViewCtrl is dealloc");
}
@end
 |