{"id":385,"date":"2015-02-26T16:02:11","date_gmt":"2015-02-26T08:02:11","guid":{"rendered":"http:\/\/www.yusian.com\/blog\/?p=385"},"modified":"2016-07-14T22:44:12","modified_gmt":"2016-07-14T14:44:12","slug":"%e6%a8%a1%e6%8b%9fios%e8%87%aa%e5%b8%a6%e7%85%a7%e7%89%87%e8%af%bb%e5%8f%96%e5%99%a8%ef%bc%8c%e8%87%aa%e5%ae%9a%e4%b9%89%e7%85%a7%e7%89%87%e6%b5%8f%e8%a7%88%e5%99%a8","status":"publish","type":"post","link":"https:\/\/www.yusian.com\/blog\/project\/2015\/02\/26\/160211385.html","title":{"rendered":"\u6a21\u62dfiOS\u81ea\u5e26\u7167\u7247\u8bfb\u53d6\u5668\uff0c\u81ea\u5b9a\u4e49\u7167\u7247\u6d4f\u89c8\u5668"},"content":{"rendered":"<p><strong>1\u3001\u6548\u679c\u793a\u610f\u56fe\uff1a<\/strong><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.yusian.com\/blog\/wp-content\/uploads\/2016\/07\/IMG_0893.png\" alt=\"IMG_0893\" width=\"320\" height=\"568\" class=\"alignnone size-full wp-image-386\" \/><\/p>\n<p><strong>2\u3001\u4f7f\u7528\u65b9\u6cd5<\/strong><br \/>\n2.1\u3001\u521b\u5efa\u4e00\u4e2a\u5bfc\u822a\u63a7\u5236\u5668<br \/>\n2.2\u3001\u8bbe\u7f6e\u7167\u7247\u6240\u5728\u76ee\u5f55<br \/>\n2.3\u3001\u8bbe\u7f6e\u63a5\u6536\u8fd4\u56de\u7684\u56fe\u7247\u8def\u5f84<\/p>\n<pre lang=\"objc\" line=\"1\">NSString *path = [[SACache imageCachesFolder] stringByAppendingPathComponent:@\"20150226\"];\nSAImagePickViewCtrl *imagePick = [[SAImagePickViewCtrl alloc] initWithFoldPath:path];\nimagePick.resultBlock = ^(NSString *result){\n????\/\/ \u9009\u4e2d\u7167\u7247\u540e\u6267\u884c\n};\nUINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:imagePick];\n[self presentViewController:nav animated:YES completion:nil];<\/pre>\n<p><strong>3\u3001\u4ee3\u7801\u53c2\u8003<\/strong><!--more--><br \/>\nSAImagePickViewCtrl.h<\/p>\n<pre lang=\"objc\" line=\"1\">\/\/\n\/\/? SAImagePickViewCtrl.h\n\/\/\n\/\/? Created by yusian on 15-2-26.\n\/\/? Copyright (c) 2015\u5e74 Sian. All rights reserved.\n\/\/\n?\n#import <UIKit\/UIKit.h>\n?\ntypedef void (^SAImagePickViewBlock)(NSString *result);\n?\n@interface SAImagePickViewCtrl : UICollectionViewController\n?\n@property (nonatomic, copy) SAImagePickViewBlock resultBlock;\n?\n- (instancetype)initWithFoldPath:(NSString *)path;\n?\n@end<\/pre>\n<p>SAImagePickViewCtrl.m<\/p>\n<pre lang=\"objc\" line=\"1\">\/\/\n\/\/? SAImagePickViewCtrl.m\n\/\/\n\/\/? Created by yusian on 15-2-26.\n\/\/? Copyright (c) 2015\u5e74 Sian. All rights reserved.\n\/\/\n?\n#import \"SAImagePickViewCtrl.h\"\n?\n@interface SAImagePickViewCtrl ()\n@property (nonatomic, strong) NSString? *path;\n@property (nonatomic, strong) NSArray?? *images;\n@property (nonatomic, strong) NSArray?? *imagePaths;\n@end\n?\n@implementation SAImagePickViewCtrl\n?\n- (instancetype)initWithFoldPath:(NSString *)path\n{\n????self.title = @\"\u7167\u7247\u9009\u62e9\";\n????self.path = path;\n????\/\/ 1.\u6d41\u6c34\u5e03\u5c40\n????UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];\n????\/\/ 2.\u6bcf\u4e2acell\u7684\u5c3a\u5bf8\n????CGFloat width = SAScreenSize.width \/ 4;\n????layout.itemSize = CGSizeMake(width - 5, width - 5);\n????\/\/ 3.\u8bbe\u7f6ecell\u4e4b\u95f4\u7684\u6c34\u5e73\u95f4\u8ddd\n????layout.minimumInteritemSpacing = 0;\n????\/\/ 4.\u8bbe\u7f6ecell\u4e4b\u95f4\u7684\u5782\u76f4\u95f4\u8ddd\n????layout.minimumLineSpacing = 5;\n????\/\/ 5.\u8bbe\u7f6e\u56db\u5468\u7684\u5185\u8fb9\u8ddd\n????layout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);\n?????\n????return [super initWithCollectionViewLayout:layout];\n}\n?\n\/\/ \u6587\u4ef6\u5939\u8def\u5f84\n- (void)setPath:(NSString *)path\n{\n????_path = path;\n????NSError *error = nil;\n????NSFileManager *fileManager = [[NSFileManager alloc] init];\n????self.imagePaths = [fileManager contentsOfDirectoryAtPath:path error:&error];\n????if (error) SALog(@\"%@\", error.localizedDescription);\n}\n?\n\/\/ \u6587\u4ef6\u6570\u7ec4\n- (void)setImagePaths:(NSArray *)imagePaths\n{\n????NSMutableArray *array = [NSMutableArray array];\n????for (NSString *string in imagePaths) {\n????????NSString *imagePath = [self.path stringByAppendingPathComponent:string];\n????????[array addObject:imagePath];\n????}\n????self.images = array;\n????_imagePaths = array;\n}\n?\n\/\/ \u56fe\u7247\u6570\u7ec4\n- (void)setImages:(NSArray *)images\n{\n????NSMutableArray *array = [NSMutableArray array];\n????for (NSString *path in images) {\n????????UIImage *image = [UIImage imageWithContentsOfFile:path];\n????????[array addObject:image];\n????}\n????_images = array;\n}\n?\n- (void)viewDidLoad\n{\n????[super viewDidLoad];\n????[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@\"SAImagePickImage\"];\n????self.collectionView.backgroundColor = SAGrayColor(0.95);\n????UIBarButtonItem *cancel = [UIBarButtonItem buttonWithTitle:@\"\u53d6\u6d88\" target:self action:@selector(cancel)];\n????self.navigationItem.leftBarButtonItem = cancel;\n}\n?\n\/\/ \u5355\u5143\u683c\u6570\n- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section\n{\n????return self.images.count;\n}\n?\n\/\/ \u5355\u5143\u683c\u5185\u5bb9\n- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath\n{\n????UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@\"SAImagePickImage\" forIndexPath:indexPath];\n????cell.backgroundView = [[UIImageView alloc] initWithImage:self.images[indexPath.row]];\n????return cell;\n}\n?\n\/\/ \u70b9\u51fb\u4e8b\u4ef6\n- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath\n{\n????if (self.resultBlock){\n????????self.resultBlock(self.imagePaths[indexPath.row]);\n????}\n????[self cancel];\n????SALog(@\"%@\", self.imagePaths[indexPath.row]);\n}\n?\n- (void)cancel\n{\n????[self dismissViewControllerAnimated:YES completion:nil];\n}\n?\n- (void)dealloc\n{\n????SALog(@\"SAImagePickViewCtrl is dealloc\");\n}\n@end<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>1\u3001\u6548\u679c\u793a\u610f\u56fe\uff1a 2\u3001\u4f7f\u7528\u65b9\u6cd5 2.1\u3001\u521b\u5efa\u4e00\u4e2a\u5bfc\u822a\u63a7\u5236\u5668 2.2\u3001\u8bbe\u7f6e\u7167\u7247\u6240\u5728\u76ee\u5f55 2.3\u3001\u8bbe\u7f6e\u63a5\u6536\u8fd4\u56de\u7684\u56fe\u7247\u8def\u5f84 NSString *path = [[SACache imageCachesFolder] stringByAppendingPathComponent:@&#8221;20150226&#8243;]; SAImagePickViewCtrl *imagePick = [[SAImagePickViewCtrl alloc] initWithFoldPath:path]; imagePick.resultBlock = ^(NSString *result){ ????\/\/ \u9009\u4e2d\u7167\u7247\u540e\u6267\u884c }; UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:imagePick]; [self presentViewController:nav animated:YES completion:nil]; 3\u3001\u4ee3\u7801\u53c2\u8003<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[15],"class_list":["post-385","post","type-post","status-publish","format-standard","hentry","category-project","tag-module"],"_links":{"self":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts\/385","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/comments?post=385"}],"version-history":[{"count":0,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts\/385\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/media?parent=385"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/categories?post=385"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/tags?post=385"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}