年年有"余"

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 2026|回复: 0

模拟iOS自带照片读取器

[复制链接]
  • TA的每日心情

    2024-10-15 10:05
  • 签到天数: 372 天

    [LV.9]以坛为家II

    发表于 2015-2-26 16:02:40 | 显示全部楼层 |阅读模式
    本帖最后由 Sian 于 2015-2-26 16:03 编辑

    1、效果示意图:
    IMG_0893.PNG

    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
    [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
    
    SAImagePickViewCtrl.m
    [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
    

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    手机版|小黑屋|Archiver|iOS开发笔记 ( 湘ICP备14010846号 )

    GMT+8, 2024-11-11 03:46 , Processed in 0.053157 second(s), 25 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2021, Tencent Cloud.

    快速回复 返回顶部 返回列表