模拟iOS自带照片读取器
本帖最后由 Sian 于 2015-2-26 16:03 编辑1、效果示意图:
2、使用方法
2.1、创建一个导航控制器
2.2、设置照片所在目录
2.3、设置接收返回的图片路径
NSString *path = [ stringByAppendingPathComponent:@"20150226"];
SAImagePickViewCtrl *imagePick = [ initWithFoldPath:path];
imagePick.resultBlock = ^(NSString *result){
// 选中照片后执行
};
UINavigationController *nav = [ initWithRootViewController:imagePick];
;
3、代码参考
SAImagePickViewCtrl.h
//
//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
//
//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 = [ 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 ;
}
// 文件夹路径
- (void)setPath:(NSString *)path
{
_path = path;
NSError *error = nil;
NSFileManager *fileManager = [ init];
self.imagePaths = ;
if (error) SALog(@"%@", error.localizedDescription);
}
// 文件数组
- (void)setImagePaths:(NSArray *)imagePaths
{
NSMutableArray *array = ;
for (NSString *string in imagePaths) {
NSString *imagePath = ;
;
}
self.images = array;
_imagePaths = array;
}
// 图片数组
- (void)setImages:(NSArray *)images
{
NSMutableArray *array = ;
for (NSString *path in images) {
UIImage *image = ;
;
}
_images = array;
}
- (void)viewDidLoad
{
;
forCellWithReuseIdentifier:@"SAImagePickImage"];
self.collectionView.backgroundColor = SAGrayColor(0.95);
UIBarButtonItem *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 = ;
cell.backgroundView = [ initWithImage:self.images];
return cell;
}
// 点击事件
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
if (self.resultBlock){
self.resultBlock(self.imagePaths);
}
;
SALog(@"%@", self.imagePaths);
}
- (void)cancel
{
;
}
- (void)dealloc
{
SALog(@"SAImagePickViewCtrl is dealloc");
}
@end
页:
[1]