模拟iOS自带照片读取器,自定义照片浏览器

1、效果示意图:

IMG_0893

2、使用方法
2.1、创建一个导航控制器
2.2、设置照片所在目录
2.3、设置接收返回的图片路径

1
2
3
4
5
6
7
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//
//? 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

SAImagePickViewCtrl.m

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
//
//? 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

Leave a Reply