├── .DS_Store ├── YPhotoSelect ├── .DS_Store ├── YPhoto.bundle │ ├── 111.png │ ├── black.png │ ├── y_icon_image_no@2x.png │ ├── y_icon_image_no@3x.png │ ├── y_icon_image_yes@2x.png │ └── y_icon_image_yes@3x.png ├── YPhotoNavViewController.h ├── YPhotoAblumTableViewController.h ├── YPhotoCollectionViewCell.h ├── YPhotoThumbnailsViewController.h ├── YPhotoPreviewViewController.h ├── YPhotoGlobalVar.m ├── YPhotoGlobalVar.h ├── YPhotoCollectionViewCell.m ├── YPhotoNavViewController.m ├── YPhotoCollectionViewCell.xib ├── YPhotoAblumTableViewController.m ├── YPhotoThumbnailsViewController.m └── YPhotoPreviewViewController.m ├── YPhotoSelectDemo ├── .DS_Store ├── 选择照片 │ ├── .DS_Store │ ├── Assets.xcassets │ │ ├── Contents.json │ │ ├── image1.imageset │ │ │ ├── image1.jpg │ │ │ └── Contents.json │ │ └── AppIcon.appiconset │ │ │ └── Contents.json │ ├── YPhotoSelect │ │ ├── .DS_Store │ │ ├── YPhoto.bundle │ │ │ ├── 111.png │ │ │ ├── black.png │ │ │ ├── y_icon_image_no@2x.png │ │ │ ├── y_icon_image_no@3x.png │ │ │ ├── y_icon_image_yes@2x.png │ │ │ └── y_icon_image_yes@3x.png │ │ ├── YPhotoNavViewController.h │ │ ├── YPhotoAblumTableViewController.h │ │ ├── YPhotoCollectionViewCell.h │ │ ├── YPhotoThumbnailsViewController.h │ │ ├── YPhotoPreviewViewController.h │ │ ├── YPhotoGlobalVar.m │ │ ├── YPhotoGlobalVar.h │ │ ├── YPhotoCollectionViewCell.m │ │ ├── YPhotoNavViewController.m │ │ ├── YPhotoCollectionViewCell.xib │ │ ├── YPhotoAblumTableViewController.m │ │ ├── YPhotoThumbnailsViewController.m │ │ └── YPhotoPreviewViewController.m │ ├── AppDelegate.h │ ├── TestCollectionViewCell.h │ ├── ViewController.h │ ├── main.m │ ├── TestCollectionViewCell.m │ ├── Info.plist │ ├── AppDelegate.m │ ├── Base.lproj │ │ └── LaunchScreen.storyboard │ ├── ViewController.m │ └── YPhotoPreviewViewController副本.m ├── 选择照片.xcodeproj │ ├── project.xcworkspace │ │ ├── xcuserdata │ │ │ └── apple.xcuserdatad │ │ │ │ └── UserInterfaceState.xcuserstate │ │ └── contents.xcworkspacedata │ ├── xcuserdata │ │ └── apple.xcuserdatad │ │ │ ├── xcschemes │ │ │ ├── xcschememanagement.plist │ │ │ └── 选择照片.xcscheme │ │ │ └── xcdebugger │ │ │ └── Breakpoints_v2.xcbkptlist │ └── project.pbxproj └── YPhoto.storyboard └── README.md /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huyp/YPhotoSelect/HEAD/.DS_Store -------------------------------------------------------------------------------- /YPhotoSelect/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huyp/YPhotoSelect/HEAD/YPhotoSelect/.DS_Store -------------------------------------------------------------------------------- /YPhotoSelectDemo/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huyp/YPhotoSelect/HEAD/YPhotoSelectDemo/.DS_Store -------------------------------------------------------------------------------- /YPhotoSelectDemo/选择照片/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huyp/YPhotoSelect/HEAD/YPhotoSelectDemo/选择照片/.DS_Store -------------------------------------------------------------------------------- /YPhotoSelect/YPhoto.bundle/111.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huyp/YPhotoSelect/HEAD/YPhotoSelect/YPhoto.bundle/111.png -------------------------------------------------------------------------------- /YPhotoSelect/YPhoto.bundle/black.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huyp/YPhotoSelect/HEAD/YPhotoSelect/YPhoto.bundle/black.png -------------------------------------------------------------------------------- /YPhotoSelectDemo/选择照片/Assets.xcassets/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "info" : { 3 | "version" : 1, 4 | "author" : "xcode" 5 | } 6 | } -------------------------------------------------------------------------------- /YPhotoSelectDemo/选择照片/YPhotoSelect/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huyp/YPhotoSelect/HEAD/YPhotoSelectDemo/选择照片/YPhotoSelect/.DS_Store -------------------------------------------------------------------------------- /YPhotoSelect/YPhoto.bundle/y_icon_image_no@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huyp/YPhotoSelect/HEAD/YPhotoSelect/YPhoto.bundle/y_icon_image_no@2x.png -------------------------------------------------------------------------------- /YPhotoSelect/YPhoto.bundle/y_icon_image_no@3x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huyp/YPhotoSelect/HEAD/YPhotoSelect/YPhoto.bundle/y_icon_image_no@3x.png -------------------------------------------------------------------------------- /YPhotoSelect/YPhoto.bundle/y_icon_image_yes@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huyp/YPhotoSelect/HEAD/YPhotoSelect/YPhoto.bundle/y_icon_image_yes@2x.png -------------------------------------------------------------------------------- /YPhotoSelect/YPhoto.bundle/y_icon_image_yes@3x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huyp/YPhotoSelect/HEAD/YPhotoSelect/YPhoto.bundle/y_icon_image_yes@3x.png -------------------------------------------------------------------------------- /YPhotoSelectDemo/选择照片/YPhotoSelect/YPhoto.bundle/111.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huyp/YPhotoSelect/HEAD/YPhotoSelectDemo/选择照片/YPhotoSelect/YPhoto.bundle/111.png -------------------------------------------------------------------------------- /YPhotoSelectDemo/选择照片/YPhotoSelect/YPhoto.bundle/black.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huyp/YPhotoSelect/HEAD/YPhotoSelectDemo/选择照片/YPhotoSelect/YPhoto.bundle/black.png -------------------------------------------------------------------------------- /YPhotoSelectDemo/选择照片/Assets.xcassets/image1.imageset/image1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huyp/YPhotoSelect/HEAD/YPhotoSelectDemo/选择照片/Assets.xcassets/image1.imageset/image1.jpg -------------------------------------------------------------------------------- /YPhotoSelectDemo/选择照片/YPhotoSelect/YPhoto.bundle/y_icon_image_no@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huyp/YPhotoSelect/HEAD/YPhotoSelectDemo/选择照片/YPhotoSelect/YPhoto.bundle/y_icon_image_no@2x.png -------------------------------------------------------------------------------- /YPhotoSelectDemo/选择照片/YPhotoSelect/YPhoto.bundle/y_icon_image_no@3x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huyp/YPhotoSelect/HEAD/YPhotoSelectDemo/选择照片/YPhotoSelect/YPhoto.bundle/y_icon_image_no@3x.png -------------------------------------------------------------------------------- /YPhotoSelectDemo/选择照片/YPhotoSelect/YPhoto.bundle/y_icon_image_yes@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huyp/YPhotoSelect/HEAD/YPhotoSelectDemo/选择照片/YPhotoSelect/YPhoto.bundle/y_icon_image_yes@2x.png -------------------------------------------------------------------------------- /YPhotoSelectDemo/选择照片/YPhotoSelect/YPhoto.bundle/y_icon_image_yes@3x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huyp/YPhotoSelect/HEAD/YPhotoSelectDemo/选择照片/YPhotoSelect/YPhoto.bundle/y_icon_image_yes@3x.png -------------------------------------------------------------------------------- /YPhotoSelectDemo/选择照片.xcodeproj/project.xcworkspace/xcuserdata/apple.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huyp/YPhotoSelect/HEAD/YPhotoSelectDemo/选择照片.xcodeproj/project.xcworkspace/xcuserdata/apple.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /YPhotoSelectDemo/选择照片.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /YPhotoSelect/YPhotoNavViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // YPhotoNavViewController.h 3 | // 选择照片 4 | // 5 | // Created by 彦鹏 on 16/7/21. 6 | // Copyright © 2016年 Huyp. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface YPhotoNavViewController : UINavigationController 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /YPhotoSelect/YPhotoAblumTableViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.h 3 | // 选择照片 4 | // 5 | // Created by 彦鹏 on 16/7/20. 6 | // Copyright © 2016年 Huyp. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface YPhotoAblumTableViewController : UITableViewController 12 | 13 | 14 | @end 15 | 16 | -------------------------------------------------------------------------------- /YPhotoSelectDemo/选择照片/YPhotoSelect/YPhotoNavViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // YPhotoNavViewController.h 3 | // 选择照片 4 | // 5 | // Created by 彦鹏 on 16/7/21. 6 | // Copyright © 2016年 Huyp. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface YPhotoNavViewController : UINavigationController 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /YPhotoSelectDemo/选择照片/AppDelegate.h: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.h 3 | // 选择照片 4 | // 5 | // Created by 彦鹏 on 16/7/20. 6 | // Copyright © 2016年 Huyp. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface AppDelegate : UIResponder 12 | 13 | @property (strong, nonatomic) UIWindow *window; 14 | 15 | 16 | @end 17 | 18 | -------------------------------------------------------------------------------- /YPhotoSelectDemo/选择照片/TestCollectionViewCell.h: -------------------------------------------------------------------------------- 1 | // 2 | // TestCollectionViewCell.h 3 | // 选择照片 4 | // 5 | // Created by 彦鹏 on 16/9/23. 6 | // Copyright © 2016年 Huyp. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface TestCollectionViewCell : UICollectionViewCell 12 | @property (strong,nonatomic) UIImageView * imageView; 13 | @end 14 | -------------------------------------------------------------------------------- /YPhotoSelectDemo/选择照片/YPhotoSelect/YPhotoAblumTableViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.h 3 | // 选择照片 4 | // 5 | // Created by 彦鹏 on 16/7/20. 6 | // Copyright © 2016年 Huyp. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface YPhotoAblumTableViewController : UITableViewController 12 | 13 | 14 | @end 15 | 16 | -------------------------------------------------------------------------------- /YPhotoSelectDemo/选择照片/ViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.h 3 | // 选择照片 4 | // 5 | // Created by 彦鹏 on 16/7/20. 6 | // Copyright © 2016年 Huyp. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "TestCollectionViewCell.h" 11 | 12 | @interface ViewController : UIViewController 13 | 14 | @end 15 | -------------------------------------------------------------------------------- /YPhotoSelectDemo/选择照片/main.m: -------------------------------------------------------------------------------- 1 | // 2 | // main.m 3 | // 选择照片 4 | // 5 | // Created by 彦鹏 on 16/7/20. 6 | // Copyright © 2016年 Huyp. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "AppDelegate.h" 11 | 12 | int main(int argc, char * argv[]) { 13 | @autoreleasepool { 14 | return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /YPhotoSelectDemo/选择照片/Assets.xcassets/image1.imageset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "universal", 5 | "filename" : "image1.jpg", 6 | "scale" : "1x" 7 | }, 8 | { 9 | "idiom" : "universal", 10 | "scale" : "2x" 11 | }, 12 | { 13 | "idiom" : "universal", 14 | "scale" : "3x" 15 | } 16 | ], 17 | "info" : { 18 | "version" : 1, 19 | "author" : "xcode" 20 | } 21 | } -------------------------------------------------------------------------------- /YPhotoSelectDemo/选择照片/TestCollectionViewCell.m: -------------------------------------------------------------------------------- 1 | // 2 | // TestCollectionViewCell.m 3 | // 选择照片 4 | // 5 | // Created by 彦鹏 on 16/9/23. 6 | // Copyright © 2016年 Huyp. All rights reserved. 7 | // 8 | 9 | #import "TestCollectionViewCell.h" 10 | 11 | @implementation TestCollectionViewCell 12 | - (instancetype)initWithFrame:(CGRect)frame { 13 | self = [super initWithFrame:frame]; 14 | 15 | if (self) { 16 | self.imageView = [[UIImageView alloc]init]; 17 | self.imageView.frame = CGRectMake(0, 0, 80, 80); 18 | [self addSubview:self.imageView]; 19 | } 20 | 21 | return self; 22 | } 23 | @end 24 | -------------------------------------------------------------------------------- /YPhotoSelectDemo/选择照片.xcodeproj/xcuserdata/apple.xcuserdatad/xcschemes/xcschememanagement.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | SchemeUserState 6 | 7 | 选择照片.xcscheme 8 | 9 | orderHint 10 | 0 11 | 12 | 13 | SuppressBuildableAutocreation 14 | 15 | 18CD2F721D3F1DEF004BC816 16 | 17 | primary 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /YPhotoSelect/YPhotoCollectionViewCell.h: -------------------------------------------------------------------------------- 1 | // 2 | // YPhotoCollectionViewCell.h 3 | // 选择照片 4 | // 5 | // Created by 彦鹏 on 16/7/21. 6 | // Copyright © 2016年 Huyp. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface YPhotoCollectionViewCell : UICollectionViewCell 12 | 13 | @property (assign,nonatomic) BOOL isPicked; 14 | 15 | typedef void (^SelectBlock)(BOOL isSelect); 16 | @property (strong,nonatomic) SelectBlock selectBlock; 17 | 18 | typedef void (^TapBlock)(); 19 | @property (strong,nonatomic) TapBlock tapBlock; 20 | 21 | @property (weak, nonatomic) IBOutlet UIImageView *imageView; 22 | @property (weak, nonatomic) IBOutlet UIButton *button; 23 | 24 | @end 25 | -------------------------------------------------------------------------------- /YPhotoSelect/YPhotoThumbnailsViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // YPhotothumbnailsViewController.h 3 | // 选择照片 4 | // 5 | // Created by 彦鹏 on 16/7/20. 6 | // Copyright © 2016年 Huyp. All rights reserved. 7 | // 8 | 9 | #import 10 | #import 11 | #import "YPhotoCollectionViewCell.h" 12 | 13 | @interface YPhotoThumbnailsViewController : UIViewController 14 | 15 | @property (strong,nonatomic) ALAssetsLibrary * lib; //防止过期 16 | @property (strong,nonatomic) ALAssetsGroup * album; 17 | 18 | typedef void (^SelectImageBlock)(NSMutableArray * imgs); 19 | 20 | @end 21 | -------------------------------------------------------------------------------- /YPhotoSelectDemo/选择照片/YPhotoSelect/YPhotoCollectionViewCell.h: -------------------------------------------------------------------------------- 1 | // 2 | // YPhotoCollectionViewCell.h 3 | // 选择照片 4 | // 5 | // Created by 彦鹏 on 16/7/21. 6 | // Copyright © 2016年 Huyp. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface YPhotoCollectionViewCell : UICollectionViewCell 12 | 13 | @property (assign,nonatomic) BOOL isPicked; 14 | 15 | typedef void (^SelectBlock)(BOOL isSelect); 16 | @property (strong,nonatomic) SelectBlock selectBlock; 17 | 18 | typedef void (^TapBlock)(); 19 | @property (strong,nonatomic) TapBlock tapBlock; 20 | 21 | @property (weak, nonatomic) IBOutlet UIImageView *imageView; 22 | @property (weak, nonatomic) IBOutlet UIButton *button; 23 | 24 | @end 25 | -------------------------------------------------------------------------------- /YPhotoSelectDemo/选择照片/YPhotoSelect/YPhotoThumbnailsViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // YPhotothumbnailsViewController.h 3 | // 选择照片 4 | // 5 | // Created by 彦鹏 on 16/7/20. 6 | // Copyright © 2016年 Huyp. All rights reserved. 7 | // 8 | 9 | #import 10 | #import 11 | #import "YPhotoCollectionViewCell.h" 12 | 13 | @interface YPhotoThumbnailsViewController : UIViewController 14 | 15 | @property (strong,nonatomic) ALAssetsLibrary * lib; //防止过期 16 | @property (strong,nonatomic) ALAssetsGroup * album; 17 | 18 | typedef void (^SelectImageBlock)(NSMutableArray * imgs); 19 | 20 | @end 21 | -------------------------------------------------------------------------------- /YPhotoSelect/YPhotoPreviewViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // YPhotoPreviewViewController.h 3 | // 选择照片 4 | // 5 | // Created by 彦鹏 on 16/7/25. 6 | // Copyright © 2016年 Huyp. All rights reserved. 7 | // 8 | 9 | #import 10 | #import 11 | 12 | @interface YPhotoPreviewViewController : UIViewController 13 | @property (strong,nonatomic) __block UIImage * centerImage; 14 | @property (strong,nonatomic) ALAssetsLibrary * lib; //防止过期 15 | @property (strong,nonatomic) NSMutableArray * aLAssets; 16 | @property (strong,nonatomic) NSIndexPath * indexPath; 17 | @property (assign,nonatomic) BOOL isPresent; 18 | @property (copy,nonatomic) NSMutableArray * presentIndexParhs; 19 | //@property (copy,nonatomic) NSMutableArray * presentAssets; 20 | 21 | @end 22 | -------------------------------------------------------------------------------- /YPhotoSelectDemo/选择照片/YPhotoSelect/YPhotoPreviewViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // YPhotoPreviewViewController.h 3 | // 选择照片 4 | // 5 | // Created by 彦鹏 on 16/7/25. 6 | // Copyright © 2016年 Huyp. All rights reserved. 7 | // 8 | 9 | #import 10 | #import 11 | 12 | @interface YPhotoPreviewViewController : UIViewController 13 | @property (strong,nonatomic) __block UIImage * centerImage; 14 | @property (strong,nonatomic) ALAssetsLibrary * lib; //防止过期 15 | @property (strong,nonatomic) NSMutableArray * aLAssets; 16 | @property (strong,nonatomic) NSIndexPath * indexPath; 17 | @property (assign,nonatomic) BOOL isPresent; 18 | @property (copy,nonatomic) NSMutableArray * presentIndexParhs; 19 | //@property (copy,nonatomic) NSMutableArray * presentAssets; 20 | 21 | @end 22 | -------------------------------------------------------------------------------- /YPhotoSelectDemo/选择照片/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "20x20", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "20x20", 11 | "scale" : "3x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "29x29", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "size" : "29x29", 21 | "scale" : "3x" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "size" : "40x40", 26 | "scale" : "2x" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "size" : "40x40", 31 | "scale" : "3x" 32 | }, 33 | { 34 | "idiom" : "iphone", 35 | "size" : "60x60", 36 | "scale" : "2x" 37 | }, 38 | { 39 | "idiom" : "iphone", 40 | "size" : "60x60", 41 | "scale" : "3x" 42 | } 43 | ], 44 | "info" : { 45 | "version" : 1, 46 | "author" : "xcode" 47 | } 48 | } -------------------------------------------------------------------------------- /YPhotoSelect/YPhotoGlobalVar.m: -------------------------------------------------------------------------------- 1 | // 2 | // YPhotoGlobalVar.m 3 | // 选择照片 4 | // 5 | // Created by 彦鹏 on 16/8/23. 6 | // Copyright © 2016年 Huyp. All rights reserved. 7 | // 8 | 9 | #import "YPhotoGlobalVar.h" 10 | 11 | @implementation YPhotoGlobalVar 12 | 13 | static id _yPhotoGlobalVar; 14 | 15 | + (instancetype)allocWithZone:(struct _NSZone *)zone 16 | { 17 | @synchronized(self) 18 | { 19 | if (_yPhotoGlobalVar==nil) 20 | { 21 | _yPhotoGlobalVar = [super allocWithZone:zone]; 22 | } 23 | } 24 | return _yPhotoGlobalVar; 25 | } 26 | + (instancetype)shareGlobalVar 27 | { 28 | if (_yPhotoGlobalVar == nil) //防止频繁加锁 29 | { 30 | @synchronized(self) 31 | { 32 | if (_yPhotoGlobalVar==nil)//防止创建多次 33 | { 34 | _yPhotoGlobalVar = [[self alloc]init]; 35 | } 36 | } 37 | } 38 | return _yPhotoGlobalVar; 39 | } 40 | - (id)copyWithZone:(NSZone *)zone 41 | { 42 | return _yPhotoGlobalVar; 43 | } 44 | 45 | @end 46 | -------------------------------------------------------------------------------- /YPhotoSelectDemo/选择照片/YPhotoSelect/YPhotoGlobalVar.m: -------------------------------------------------------------------------------- 1 | // 2 | // YPhotoGlobalVar.m 3 | // 选择照片 4 | // 5 | // Created by 彦鹏 on 16/8/23. 6 | // Copyright © 2016年 Huyp. All rights reserved. 7 | // 8 | 9 | #import "YPhotoGlobalVar.h" 10 | 11 | @implementation YPhotoGlobalVar 12 | 13 | static id _yPhotoGlobalVar; 14 | 15 | + (instancetype)allocWithZone:(struct _NSZone *)zone 16 | { 17 | @synchronized(self) 18 | { 19 | if (_yPhotoGlobalVar==nil) 20 | { 21 | _yPhotoGlobalVar = [super allocWithZone:zone]; 22 | } 23 | } 24 | return _yPhotoGlobalVar; 25 | } 26 | + (instancetype)shareGlobalVar 27 | { 28 | if (_yPhotoGlobalVar == nil) //防止频繁加锁 29 | { 30 | @synchronized(self) 31 | { 32 | if (_yPhotoGlobalVar==nil)//防止创建多次 33 | { 34 | _yPhotoGlobalVar = [[self alloc]init]; 35 | } 36 | } 37 | } 38 | return _yPhotoGlobalVar; 39 | } 40 | - (id)copyWithZone:(NSZone *)zone 41 | { 42 | return _yPhotoGlobalVar; 43 | } 44 | 45 | @end 46 | -------------------------------------------------------------------------------- /YPhotoSelect/YPhotoGlobalVar.h: -------------------------------------------------------------------------------- 1 | // 2 | // YPhotoGlobalVar.h 3 | // 选择照片 4 | // 5 | // Created by 彦鹏 on 16/8/23. 6 | // Copyright © 2016年 Huyp. All rights reserved. 7 | // 8 | 9 | #import 10 | #import 11 | 12 | @interface YPhotoGlobalVar : NSObject 13 | /*! 14 | 设置的最大照片数和返回的全屏图和缩略图在这里. 15 | */ 16 | @property (assign,nonatomic) int maxNum; 17 | @property (assign,nonatomic) int currentNum; 18 | @property (strong,nonatomic) NSMutableArray * selectedImgs; 19 | @property (strong,nonatomic) NSMutableArray * selectedThumbnails; 20 | @property (strong,nonatomic) NSMutableArray * selectedAlassets; 21 | @property (strong,nonatomic) NSMutableArray * indexPaths; 22 | @property (strong,nonatomic) NSMutableArray * reloadIndexPaths; 23 | @property (strong,nonatomic) CAKeyframeAnimation * animation; 24 | @property (assign,nonatomic) CGFloat targetImageScale; 25 | @property (assign,nonatomic) BOOL png; //default is NO; imgStyle is jpg 26 | /*! 27 | 每次读取的时候直接从内存提取,不用再每次根据名字读图片了; 28 | */ 29 | @property (strong,nonatomic) UIImage * yesImg; 30 | @property (strong,nonatomic) UIImage * noImg; 31 | 32 | + (instancetype)shareGlobalVar; 33 | 34 | @end 35 | -------------------------------------------------------------------------------- /YPhotoSelectDemo/选择照片/YPhotoSelect/YPhotoGlobalVar.h: -------------------------------------------------------------------------------- 1 | // 2 | // YPhotoGlobalVar.h 3 | // 选择照片 4 | // 5 | // Created by 彦鹏 on 16/8/23. 6 | // Copyright © 2016年 Huyp. All rights reserved. 7 | // 8 | 9 | #import 10 | #import 11 | 12 | @interface YPhotoGlobalVar : NSObject 13 | /*! 14 | 设置的最大照片数和返回的全屏图和缩略图在这里. 15 | */ 16 | @property (assign,nonatomic) int maxNum; 17 | @property (assign,nonatomic) int currentNum; 18 | @property (strong,nonatomic) NSMutableArray * selectedImgs; 19 | @property (strong,nonatomic) NSMutableArray * selectedThumbnails; 20 | @property (strong,nonatomic) NSMutableArray * selectedAlassets; 21 | @property (strong,nonatomic) NSMutableArray * indexPaths; 22 | @property (strong,nonatomic) NSMutableArray * reloadIndexPaths; 23 | @property (strong,nonatomic) CAKeyframeAnimation * animation; 24 | @property (assign,nonatomic) CGFloat targetImageScale; 25 | @property (assign,nonatomic) BOOL png; //default is NO; imgStyle is jpg 26 | /*! 27 | 每次读取的时候直接从内存提取,不用再每次根据名字读图片了; 28 | */ 29 | @property (strong,nonatomic) UIImage * yesImg; 30 | @property (strong,nonatomic) UIImage * noImg; 31 | 32 | + (instancetype)shareGlobalVar; 33 | 34 | @end 35 | -------------------------------------------------------------------------------- /YPhotoSelectDemo/选择照片/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | NSPhotoLibraryUsageDescription 6 | 访问相册 7 | CFBundleDevelopmentRegion 8 | en 9 | CFBundleExecutable 10 | $(EXECUTABLE_NAME) 11 | CFBundleIdentifier 12 | $(PRODUCT_BUNDLE_IDENTIFIER) 13 | CFBundleInfoDictionaryVersion 14 | 6.0 15 | CFBundleName 16 | $(PRODUCT_NAME) 17 | CFBundlePackageType 18 | APPL 19 | CFBundleShortVersionString 20 | 1.0 21 | CFBundleSignature 22 | ???? 23 | CFBundleVersion 24 | 1 25 | LSRequiresIPhoneOS 26 | 27 | UILaunchStoryboardName 28 | LaunchScreen 29 | UIRequiredDeviceCapabilities 30 | 31 | armv7 32 | 33 | UISupportedInterfaceOrientations 34 | 35 | UIInterfaceOrientationPortrait 36 | UIInterfaceOrientationLandscapeLeft 37 | UIInterfaceOrientationLandscapeRight 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /YPhotoSelect/YPhotoCollectionViewCell.m: -------------------------------------------------------------------------------- 1 | // 2 | // YPhotoCollectionViewCell.m 3 | // 选择照片 4 | // 5 | // Created by 彦鹏 on 16/7/21. 6 | // Copyright © 2016年 Huyp. All rights reserved. 7 | // 8 | 9 | #import "YPhotoCollectionViewCell.h" 10 | #import "YPhotoGlobalVar.h" 11 | 12 | @interface YPhotoCollectionViewCell() 13 | 14 | @property (strong,nonatomic) YPhotoGlobalVar * globalVar; 15 | 16 | @end 17 | 18 | @implementation YPhotoCollectionViewCell 19 | 20 | - (void)awakeFromNib { 21 | [super awakeFromNib]; 22 | [_button addTarget:self action:@selector(selectOrDeselect) forControlEvents:UIControlEventTouchUpInside]; 23 | [_button setImage:[UIImage imageNamed:@"YPhoto.bundle/y_icon_image_no"] forState:UIControlStateNormal]; 24 | 25 | _globalVar = [YPhotoGlobalVar shareGlobalVar]; 26 | } 27 | 28 | - (void)selectOrDeselect { 29 | 30 | if (_isPicked == NO) { 31 | if (_globalVar.maxNum == _globalVar.currentNum) { 32 | NSString * title = [NSString stringWithFormat:@"您最多只能选择%d张图片",_globalVar.maxNum]; 33 | UIAlertView * alert = [[UIAlertView alloc]initWithTitle:title message:nil delegate:nil cancelButtonTitle:@"我知道了" otherButtonTitles:nil, nil]; 34 | [alert show]; 35 | return; 36 | } 37 | 38 | if (_globalVar.animation == nil) { 39 | _globalVar.animation = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"]; 40 | // CAKeyframeAnimation(keyPath: "transform.scale"); 41 | _globalVar.animation.duration = 0.15; 42 | _globalVar.animation.autoreverses = false; 43 | _globalVar.animation.values = @[@1.0,@1.2,@1.0]; 44 | _globalVar.animation.fillMode = kCAFillModeBackwards; 45 | } 46 | 47 | _isPicked = YES; 48 | if (_selectBlock) { 49 | self.selectBlock(_isPicked); 50 | } 51 | [_button setImage:[UIImage imageNamed:@"YPhoto.bundle/y_icon_image_yes"] forState:UIControlStateNormal]; 52 | [_button.layer removeAnimationForKey:@"transform.scale"]; 53 | [_button.layer addAnimation:_globalVar.animation forKey:@"transform.scale"]; 54 | } 55 | else if (_isPicked == YES) { 56 | _isPicked = NO; 57 | if (_selectBlock) { 58 | self.selectBlock(_isPicked); 59 | } 60 | [_button setImage:[UIImage imageNamed:@"YPhoto.bundle/y_icon_image_no"] forState:UIControlStateNormal]; 61 | } 62 | } 63 | 64 | @end 65 | -------------------------------------------------------------------------------- /YPhotoSelectDemo/选择照片/YPhotoSelect/YPhotoCollectionViewCell.m: -------------------------------------------------------------------------------- 1 | // 2 | // YPhotoCollectionViewCell.m 3 | // 选择照片 4 | // 5 | // Created by 彦鹏 on 16/7/21. 6 | // Copyright © 2016年 Huyp. All rights reserved. 7 | // 8 | 9 | #import "YPhotoCollectionViewCell.h" 10 | #import "YPhotoGlobalVar.h" 11 | 12 | @interface YPhotoCollectionViewCell() 13 | 14 | @property (strong,nonatomic) YPhotoGlobalVar * globalVar; 15 | 16 | @end 17 | 18 | @implementation YPhotoCollectionViewCell 19 | 20 | - (void)awakeFromNib { 21 | [super awakeFromNib]; 22 | [_button addTarget:self action:@selector(selectOrDeselect) forControlEvents:UIControlEventTouchUpInside]; 23 | [_button setImage:[UIImage imageNamed:@"YPhoto.bundle/y_icon_image_no"] forState:UIControlStateNormal]; 24 | 25 | _globalVar = [YPhotoGlobalVar shareGlobalVar]; 26 | } 27 | 28 | - (void)selectOrDeselect { 29 | 30 | if (_isPicked == NO) { 31 | if (_globalVar.maxNum == _globalVar.currentNum) { 32 | NSString * title = [NSString stringWithFormat:@"您最多只能选择%d张图片",_globalVar.maxNum]; 33 | UIAlertView * alert = [[UIAlertView alloc]initWithTitle:title message:nil delegate:nil cancelButtonTitle:@"我知道了" otherButtonTitles:nil, nil]; 34 | [alert show]; 35 | return; 36 | } 37 | 38 | if (_globalVar.animation == nil) { 39 | _globalVar.animation = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"]; 40 | // CAKeyframeAnimation(keyPath: "transform.scale"); 41 | _globalVar.animation.duration = 0.15; 42 | _globalVar.animation.autoreverses = false; 43 | _globalVar.animation.values = @[@1.0,@1.2,@1.0]; 44 | _globalVar.animation.fillMode = kCAFillModeBackwards; 45 | } 46 | 47 | _isPicked = YES; 48 | if (_selectBlock) { 49 | self.selectBlock(_isPicked); 50 | } 51 | [_button setImage:[UIImage imageNamed:@"YPhoto.bundle/y_icon_image_yes"] forState:UIControlStateNormal]; 52 | [_button.layer removeAnimationForKey:@"transform.scale"]; 53 | [_button.layer addAnimation:_globalVar.animation forKey:@"transform.scale"]; 54 | } 55 | else if (_isPicked == YES) { 56 | _isPicked = NO; 57 | if (_selectBlock) { 58 | self.selectBlock(_isPicked); 59 | } 60 | [_button setImage:[UIImage imageNamed:@"YPhoto.bundle/y_icon_image_no"] forState:UIControlStateNormal]; 61 | } 62 | } 63 | 64 | @end 65 | -------------------------------------------------------------------------------- /YPhotoSelectDemo/选择照片/AppDelegate.m: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.m 3 | // 选择照片 4 | // 5 | // Created by 彦鹏 on 16/7/20. 6 | // Copyright © 2016年 Huyp. All rights reserved. 7 | // 8 | 9 | #import "AppDelegate.h" 10 | #import "ViewController.h" 11 | 12 | @interface AppDelegate () 13 | 14 | @end 15 | 16 | @implementation AppDelegate 17 | 18 | 19 | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 20 | self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds]; 21 | self.window.backgroundColor = [UIColor whiteColor]; 22 | UINavigationController * rootVC = [[UINavigationController alloc]initWithRootViewController:[ViewController new]]; 23 | self.window.rootViewController = rootVC; 24 | [self.window makeKeyAndVisible]; 25 | return YES; 26 | } 27 | 28 | - (void)applicationWillResignActive:(UIApplication *)application { 29 | // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 30 | // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. 31 | } 32 | 33 | - (void)applicationDidEnterBackground:(UIApplication *)application { 34 | // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 35 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 36 | } 37 | 38 | - (void)applicationWillEnterForeground:(UIApplication *)application { 39 | // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. 40 | } 41 | 42 | - (void)applicationDidBecomeActive:(UIApplication *)application { 43 | // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 44 | } 45 | 46 | - (void)applicationWillTerminate:(UIApplication *)application { 47 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 48 | } 49 | 50 | @end 51 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## YPhotoSelect 2 | 3 | * 模仿微信的照片选择器 4 | * 获取图片的缩略图和高清图 5 | * 保证浏览图片时的内存循环利用,浏览图片时内存稳定,内存占用很小.和微信一致. 6 | * 可以自定义图片数量 7 | * 可以自定义高清图片的尺寸 8 | 9 | ### 图片 10 | 11 | ![](http://images2015.cnblogs.com/blog/881202/201609/881202-20160927161002360-46403511.png) 12 | ![](http://images2015.cnblogs.com/blog/881202/201609/881202-20160927161005453-795690702.png) 13 | ![](http://images2015.cnblogs.com/blog/881202/201609/881202-20160927161009750-1488094518.png) 14 | 15 | ### 使用 16 | 17 | ##### 加载: 18 | ``` 19 | 必须要导入的头文件 : 20 | #import "YPhotoAblumTableViewController.h" 21 | #import "YPhotoNavViewController.h" 22 | #import "YPhotoGlobalVar.h" 23 | ``` 24 | 25 | ``` 26 | 需要的属性 : 27 | @property (strong,nonatomic) YPhotoGlobalVar * globalVar; //全局变量 28 | @property (strong,nonatomic) NSMutableArray * thumbnails; //接收返回的缩略图 29 | @property (strong,nonatomic) NSMutableArray * imgs; //接收返回的图片 30 | 31 | ``` 32 | 33 | ``` 34 | - (void)viewDidLoad { 35 | [super viewDidLoad]; 36 | _thumbnails = [NSMutableArray new]; 37 | _imgs = [NSMutableArray new]; 38 | //设置全局变量 39 | _globalVar = [YPhotoGlobalVar shareGlobalVar]; 40 | 41 | //图片的尺寸缩放值 照相机拍摄 default is {758, 1136} or {1136, 758} scale 0..1 42 | _globalVar.targetImageScale = 0.5; 43 | //设置图片类型 png jpg default is jpg 44 | _globalVar.png = YES; 45 | 46 | //这是自定义页面 47 | [self setView]; 48 | } 49 | 50 | - (void)viewDidAppear:(BOOL)animated { 51 | [super viewDidAppear:animated]; 52 | //以下这几步是必须的 53 | //添加图片 54 | if (_globalVar.selectedThumbnails.count >0) { 55 | [_thumbnails addObjectsFromArray:_globalVar.selectedThumbnails]; 56 | [_imgs addObject:_globalVar.selectedImgs]; 57 | } 58 | //设置还能选择的最大图片数量 第一个数字是最大数量 59 | _globalVar.maxNum = 3 - (int)_thumbnails.count; 60 | 61 | [collection reloadData]; 62 | } 63 | ``` 64 | ``` 65 | //点击按钮进入相册的方法 66 | - (void)present { 67 | //以下这几步是必须的 68 | YPhotoAblumTableViewController * y = [YPhotoAblumTableViewController new]; 69 | YPhotoNavViewController * n = [[YPhotoNavViewController alloc]initWithRootViewController:y]; 70 | //必须是present方式进入照片选择器; 71 | [self presentViewController:n animated:YES completion:nil]; 72 | } 73 | ``` 74 | ``` 75 | //如果采用了collectionView展示图片缩略图 数量这么设置 76 | - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { 77 | return _thumbnails.count; 78 | } 79 | 80 | ``` 81 | 82 | ### 提醒 83 | * 本框架纯ARC,兼容的系统>=iOS6.0、iPhone竖屏 不支持iPad 84 | * 图片浏览器已经把方向锁定在竖屏,但是不影响application的横竖屏切换 85 | 86 | * 如果在使用过程中遇到BUG,希望你能告诉我,谢谢. 我的email : huyanpeng_ios@126.com 87 | * 如果在使用过程中发现功能不够用,您可以自定义添加功能 , 也可以告诉我,我非常想为这个框架增加更多好用的功能 ,我也同样期待您的加入一起完善这个框架 谢谢 88 | * 如果您喜欢,劳烦您点个`star`! thank you ! 89 | 90 | -------------------------------------------------------------------------------- /YPhotoSelect/YPhotoNavViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // YPhotoNavViewController.m 3 | // 选择照片 4 | // 5 | // Created by 彦鹏 on 16/7/21. 6 | // Copyright © 2016年 Huyp. All rights reserved. 7 | // 8 | 9 | #import "YPhotoNavViewController.h" 10 | #import "YPhotoAblumTableViewController.h" 11 | #import "YPhotoGlobalVar.h" 12 | 13 | @interface YPhotoNavViewController () 14 | @property (strong ,nonatomic) YPhotoGlobalVar * globalVar; 15 | @end 16 | 17 | @implementation YPhotoNavViewController 18 | 19 | - (void)viewDidLoad { 20 | [super viewDidLoad]; 21 | UIImage * bgImage = [self createImageWithColor:[UIColor colorWithRed:0.1 green:0.1 blue:0.1 alpha:0.7]]; 22 | [self.navigationBar setBackgroundImage:bgImage forBarMetrics:UIBarMetricsDefault]; 23 | // self.navigationBar.barTintColor = [UIColor colorWithRed:0.1 green:0.1 blue:0.1 alpha:0.8]; //这个导航栏透明度为1(不透明); 24 | 25 | self.navigationBar.translucent = YES; 26 | self.navigationBar.alpha = 0.5; 27 | [self.navigationBar setShadowImage:[UIImage new]]; 28 | //设置统一的颜色 29 | [self.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}]; 30 | self.navigationBar.tintColor = [UIColor whiteColor]; 31 | 32 | self.interactivePopGestureRecognizer.delegate = self; 33 | 34 | _globalVar = [YPhotoGlobalVar shareGlobalVar]; 35 | _globalVar.currentNum = 0; 36 | _globalVar.selectedImgs = [NSMutableArray new]; 37 | _globalVar.selectedAlassets = [NSMutableArray new]; 38 | _globalVar.indexPaths = [NSMutableArray new]; 39 | _globalVar.selectedThumbnails = [NSMutableArray new]; 40 | _globalVar.reloadIndexPaths = [NSMutableArray new]; 41 | //不合理的值或默认状态下 缩放比例为1 42 | if (_globalVar.targetImageScale <= 0.0 ||_globalVar.targetImageScale > 1.0) { 43 | _globalVar.targetImageScale = 1.0; 44 | } 45 | _globalVar.yesImg = [[UIImage imageNamed:@"YPhoto.bundle/y_icon_image_yes"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; 46 | _globalVar.noImg = [[UIImage imageNamed:@"YPhoto.bundle/y_icon_image_no"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; 47 | } 48 | 49 | - (UIViewController *)childViewControllerForStatusBarStyle { 50 | return self.topViewController; 51 | } 52 | 53 | - (UIImage*)createImageWithColor:(UIColor*) color 54 | { 55 | CGRect rect=CGRectMake(0.0f, 0.0f, 1.0f, 1.0f); 56 | UIGraphicsBeginImageContext(rect.size); 57 | CGContextRef context = UIGraphicsGetCurrentContext(); 58 | CGContextSetFillColorWithColor(context, [color CGColor]); 59 | CGContextFillRect(context, rect); 60 | UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext(); 61 | UIGraphicsEndImageContext(); 62 | return theImage; 63 | } 64 | 65 | //锁定转向 66 | - (BOOL)shouldAutorotate { 67 | return YES; 68 | } 69 | - (UIInterfaceOrientationMask)supportedInterfaceOrientations { 70 | return UIInterfaceOrientationMaskPortrait; 71 | } 72 | 73 | @end 74 | -------------------------------------------------------------------------------- /YPhotoSelectDemo/选择照片/YPhotoSelect/YPhotoNavViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // YPhotoNavViewController.m 3 | // 选择照片 4 | // 5 | // Created by 彦鹏 on 16/7/21. 6 | // Copyright © 2016年 Huyp. All rights reserved. 7 | // 8 | 9 | #import "YPhotoNavViewController.h" 10 | #import "YPhotoAblumTableViewController.h" 11 | #import "YPhotoGlobalVar.h" 12 | 13 | @interface YPhotoNavViewController () 14 | @property (strong ,nonatomic) YPhotoGlobalVar * globalVar; 15 | @end 16 | 17 | @implementation YPhotoNavViewController 18 | 19 | - (void)viewDidLoad { 20 | [super viewDidLoad]; 21 | UIImage * bgImage = [self createImageWithColor:[UIColor colorWithRed:0.1 green:0.1 blue:0.1 alpha:0.7]]; 22 | [self.navigationBar setBackgroundImage:bgImage forBarMetrics:UIBarMetricsDefault]; 23 | // self.navigationBar.barTintColor = [UIColor colorWithRed:0.1 green:0.1 blue:0.1 alpha:0.8]; //这个导航栏透明度为1(不透明); 24 | 25 | self.navigationBar.translucent = YES; 26 | self.navigationBar.alpha = 0.5; 27 | [self.navigationBar setShadowImage:[UIImage new]]; 28 | //设置统一的颜色 29 | [self.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}]; 30 | self.navigationBar.tintColor = [UIColor whiteColor]; 31 | 32 | self.interactivePopGestureRecognizer.delegate = self; 33 | 34 | _globalVar = [YPhotoGlobalVar shareGlobalVar]; 35 | _globalVar.currentNum = 0; 36 | _globalVar.selectedImgs = [NSMutableArray new]; 37 | _globalVar.selectedAlassets = [NSMutableArray new]; 38 | _globalVar.indexPaths = [NSMutableArray new]; 39 | _globalVar.selectedThumbnails = [NSMutableArray new]; 40 | _globalVar.reloadIndexPaths = [NSMutableArray new]; 41 | //不合理的值或默认状态下 缩放比例为1 42 | if (_globalVar.targetImageScale <= 0.0 ||_globalVar.targetImageScale > 1.0) { 43 | _globalVar.targetImageScale = 1.0; 44 | } 45 | _globalVar.yesImg = [[UIImage imageNamed:@"YPhoto.bundle/y_icon_image_yes"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; 46 | _globalVar.noImg = [[UIImage imageNamed:@"YPhoto.bundle/y_icon_image_no"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; 47 | } 48 | 49 | - (UIViewController *)childViewControllerForStatusBarStyle { 50 | return self.topViewController; 51 | } 52 | 53 | - (UIImage*)createImageWithColor:(UIColor*) color 54 | { 55 | CGRect rect=CGRectMake(0.0f, 0.0f, 1.0f, 1.0f); 56 | UIGraphicsBeginImageContext(rect.size); 57 | CGContextRef context = UIGraphicsGetCurrentContext(); 58 | CGContextSetFillColorWithColor(context, [color CGColor]); 59 | CGContextFillRect(context, rect); 60 | UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext(); 61 | UIGraphicsEndImageContext(); 62 | return theImage; 63 | } 64 | 65 | //锁定转向 66 | - (BOOL)shouldAutorotate { 67 | return YES; 68 | } 69 | - (UIInterfaceOrientationMask)supportedInterfaceOrientations { 70 | return UIInterfaceOrientationMaskPortrait; 71 | } 72 | 73 | @end 74 | -------------------------------------------------------------------------------- /YPhotoSelectDemo/选择照片/Base.lproj/LaunchScreen.storyboard: -------------------------------------------------------------------------------- 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 | -------------------------------------------------------------------------------- /YPhotoSelect/YPhotoCollectionViewCell.xib: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /YPhotoSelectDemo/选择照片/YPhotoSelect/YPhotoCollectionViewCell.xib: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /YPhotoSelectDemo/选择照片.xcodeproj/xcuserdata/apple.xcuserdatad/xcschemes/选择照片.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 24 | 25 | 30 | 31 | 32 | 33 | 39 | 40 | 41 | 42 | 43 | 44 | 54 | 56 | 62 | 63 | 64 | 65 | 66 | 67 | 73 | 75 | 81 | 82 | 83 | 84 | 86 | 87 | 90 | 91 | 92 | -------------------------------------------------------------------------------- /YPhotoSelectDemo/选择照片/ViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.m 3 | // 选择照片 4 | // 5 | // Created by 彦鹏 on 16/7/20. 6 | // Copyright © 2016年 Huyp. All rights reserved. 7 | // 8 | 9 | #import "ViewController.h" 10 | #import "YPhotoAblumTableViewController.h" 11 | #import "YPhotoNavViewController.h" 12 | #import "YPhotoGlobalVar.h" 13 | 14 | 15 | @interface ViewController () 16 | 17 | { 18 | UICollectionView * collection; 19 | } 20 | 21 | @property (strong,nonatomic) YPhotoGlobalVar * globalVar; //全局变量 22 | @property (strong,nonatomic) NSMutableArray * thumbnails; //接收返回的缩略图 23 | @property (strong,nonatomic) NSMutableArray * imgs; //接收返回的图片 24 | @end 25 | 26 | @implementation ViewController 27 | 28 | /* 29 | 必须要导入的头文件 : 30 | #import "YPhotoAblumTableViewController.h" 31 | #import "YPhotoNavViewController.h" 32 | #import "YPhotoGlobalVar.h" 33 | 34 | present 和 viewDidAppear 中的方法是必须要实现的 35 | 36 | viewDidLoad中的变量有默认值,如果默认满足需求可不设置 37 | 38 | 如果使用了collectionView方式展示图片 Item的数量 使用 接收到的缩略图.count 39 | */ 40 | 41 | - (void)viewDidLoad { 42 | [super viewDidLoad]; 43 | _thumbnails = [NSMutableArray new]; 44 | _imgs = [NSMutableArray new]; 45 | //设置全局变量 46 | _globalVar = [YPhotoGlobalVar shareGlobalVar]; 47 | 48 | //图片的尺寸缩放值 照相机拍摄 default is {758, 1136} or {1136, 758} scale 0..1 49 | _globalVar.targetImageScale = 0.5; 50 | //设置图片类型 png jpg default is jpg 51 | _globalVar.png = YES; 52 | 53 | [self setView]; 54 | } 55 | 56 | - (void)viewDidAppear:(BOOL)animated { 57 | [super viewDidAppear:animated]; 58 | //以下这几部是必须的 59 | 60 | //添加图片 61 | if (_globalVar.selectedThumbnails.count >0) { 62 | [_thumbnails addObjectsFromArray:_globalVar.selectedThumbnails]; 63 | [_imgs addObject:_globalVar.selectedImgs]; 64 | } 65 | //设置还能选择的最大图片数量 第一个数字是最大数量 66 | _globalVar.maxNum = 3 - (int)_thumbnails.count; 67 | 68 | [collection reloadData]; 69 | } 70 | //点击按钮进入相册的方法 71 | - (void)present { 72 | //以下这几步是必须的 73 | YPhotoAblumTableViewController * y = [YPhotoAblumTableViewController new]; 74 | YPhotoNavViewController * n = [[YPhotoNavViewController alloc]initWithRootViewController:y]; 75 | //必须是present方式进入照片选择器; 76 | [self presentViewController:n animated:YES completion:nil]; 77 | } 78 | 79 | - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { 80 | return _thumbnails.count; 81 | } 82 | 83 | - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { 84 | static NSString * cellID = @"cell"; 85 | 86 | TestCollectionViewCell * cell = (TestCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath]; 87 | 88 | //如果使用了collectionView方式展示图片 使用接收到的缩略图 89 | cell.imageView.image = _thumbnails[indexPath.row]; 90 | 91 | return cell; 92 | } 93 | 94 | - (void)setView { 95 | UIButton * button = [[UIButton alloc]initWithFrame:CGRectMake(80, 80, 80, 80)]; 96 | [button setTitle:@"add" forState:UIControlStateNormal]; 97 | [button addTarget:self action:@selector(present) forControlEvents:UIControlEventTouchUpInside]; 98 | [button setBackgroundColor:[UIColor redColor]]; 99 | [self.view addSubview:button]; 100 | 101 | UICollectionViewFlowLayout * layout = [UICollectionViewFlowLayout new]; 102 | layout.itemSize = CGSizeMake(80, 80); 103 | layout.minimumInteritemSpacing = 2; 104 | layout.minimumLineSpacing = 2; 105 | collection = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 200, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height-200) collectionViewLayout:layout]; 106 | collection.backgroundColor = [UIColor yellowColor]; 107 | collection.delegate = self; 108 | collection.dataSource = self; 109 | [collection registerClass:[TestCollectionViewCell class] forCellWithReuseIdentifier:@"cell"]; 110 | [self.view addSubview:collection]; 111 | } 112 | 113 | - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { 114 | return 1; 115 | } 116 | 117 | - (void)didReceiveMemoryWarning { 118 | [super didReceiveMemoryWarning]; 119 | NSLog(@"memoryWarning"); 120 | // Dispose of any resources that can be recreated. 121 | } 122 | 123 | @end 124 | -------------------------------------------------------------------------------- /YPhotoSelect/YPhotoAblumTableViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.m 3 | // 选择照片 4 | // 5 | // Created by 彦鹏 on 16/7/20. 6 | // Copyright © 2016年 Huyp. All rights reserved. 7 | // 8 | 9 | #import "YPhotoAblumTableViewController.h" 10 | #import 11 | #import "YPhotothumbnailsViewController.h" 12 | 13 | @interface YPhotoAblumTableViewController () 14 | @property (strong,nonatomic) NSMutableArray * albums; 15 | @property (strong,nonatomic) NSMutableArray * thumbnails; 16 | @property (strong,nonatomic) NSMutableArray * names; 17 | @property (strong,nonatomic) NSMutableArray * amounts; 18 | @property (strong,nonatomic) ALAssetsLibrary * assetsLibrary; 19 | 20 | @end 21 | @implementation YPhotoAblumTableViewController 22 | 23 | 24 | 25 | - (UIStatusBarStyle)preferredStatusBarStyle { 26 | return UIStatusBarStyleLightContent; 27 | } 28 | 29 | - (void)cancel { 30 | [self dismissViewControllerAnimated:YES completion:nil]; 31 | } 32 | 33 | - (void)viewDidLoad { 34 | [super viewDidLoad]; 35 | 36 | self.title = @"相册"; 37 | self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(cancel)]; 38 | [self.navigationItem.rightBarButtonItem setTitleTextAttributes:@{ NSFontAttributeName : [UIFont systemFontOfSize:15]} forState:UIControlStateNormal]; 39 | 40 | UIBarButtonItem * backItem = [[UIBarButtonItem alloc]init]; 41 | backItem.title = @"相册"; 42 | [backItem setTitleTextAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:15]} forState:UIControlStateNormal]; 43 | self.navigationItem.backBarButtonItem = backItem; 44 | 45 | _albums = [[NSMutableArray alloc] init]; 46 | _thumbnails = [[NSMutableArray alloc] init]; 47 | _names = [[NSMutableArray alloc] init]; 48 | _amounts = [[NSMutableArray alloc] init]; 49 | 50 | self.tableView.delegate = self; 51 | self.tableView.dataSource = self; 52 | // self.tableView.separatorStyle = UITableViewCellSelectionStyleNone; 53 | 54 | [self loadAlbum]; 55 | } 56 | 57 | - (void)loadAlbum { 58 | // 获取当前应用对照片的访问授权状态 59 | ALAuthorizationStatus status = [ALAssetsLibrary authorizationStatus]; 60 | // 如果没有获取访问授权,或者访问授权状态已经被明确禁止,则显示提示语,引导用户开启授权 61 | if (status == ALAuthorizationStatusRestricted || status == ALAuthorizationStatusDenied) { 62 | UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"应用想访问您的相册" message:@"请设置应用的相册访问权限" delegate:self cancelButtonTitle:@"不允许" otherButtonTitles:@"去设置", nil]; 63 | [alert show]; 64 | } 65 | else { 66 | _assetsLibrary = [[ALAssetsLibrary alloc] init]; 67 | 68 | [_assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) { 69 | 70 | [group setAssetsFilter:[ALAssetsFilter allPhotos]]; 71 | if (group) { 72 | if (group.numberOfAssets > 0) { 73 | // 把相册储存到数组中,方便后面展示相册时使用 74 | [_albums addObject:group]; 75 | [_names addObject:[self getZH_CN:[group valueForProperty:ALAssetsGroupPropertyName]]]; 76 | [_amounts addObject:[NSString stringWithFormat:@"%ld",(long)[group numberOfAssets]]]; 77 | [_thumbnails addObject:[UIImage imageWithCGImage:[group posterImage]]]; 78 | } 79 | } 80 | else { 81 | [self.tableView reloadData]; 82 | } 83 | } 84 | failureBlock:^(NSError *error) { 85 | NSLog(@"Asset group not found!"); 86 | }]; 87 | } 88 | } 89 | 90 | - (NSString *)getZH_CN:(NSString *)name { 91 | if ([name isEqualToString:@"Favorites"]) { 92 | return @"个人收藏"; 93 | } 94 | else if ([name isEqualToString:@"Panoramas"]) { 95 | return @"全景照片"; 96 | } 97 | else if ([name isEqualToString:@"Camera Roll"]) { 98 | return @"相机胶卷"; 99 | } 100 | else if ([name isEqualToString:@"Screenshots"]) { 101 | return @"屏幕快照"; 102 | } 103 | else if ([name isEqualToString:@"Bursts"]) { 104 | return @"连拍快照"; 105 | } 106 | else if ([name isEqualToString:@"Selfies"]) { 107 | return @"自拍"; 108 | } 109 | else if ([name isEqualToString:@"Recently Added"]) { 110 | return @"最近添加"; 111 | } 112 | else { 113 | return name; 114 | } 115 | } 116 | 117 | - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 118 | return 1; 119 | } 120 | 121 | - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 122 | if (_albums == nil) { 123 | return 0; 124 | } 125 | else { 126 | return _albums.count; 127 | } 128 | } 129 | 130 | - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 131 | static NSString * cellID = @"album"; 132 | UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellID]; 133 | 134 | if (!cell) { 135 | cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID]; 136 | cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 137 | cell.imageView.contentMode = UIViewContentModeScaleAspectFill; 138 | } 139 | 140 | cell.imageView.image = _thumbnails[indexPath.row]; 141 | cell.textLabel.text = _names[indexPath.row]; 142 | cell.detailTextLabel.text = _amounts[indexPath.row]; 143 | 144 | return cell; 145 | } 146 | 147 | - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 148 | return [UIScreen mainScreen].bounds.size.height/9; 149 | } 150 | 151 | - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 152 | [tableView deselectRowAtIndexPath:indexPath animated:YES]; 153 | YPhotoThumbnailsViewController * photoThumbnailVC = [YPhotoThumbnailsViewController new]; 154 | photoThumbnailVC.album = _albums[indexPath.row]; 155 | photoThumbnailVC.lib = _assetsLibrary; 156 | [self.navigationController pushViewController:photoThumbnailVC animated:YES]; 157 | } 158 | 159 | - (void)didReceiveMemoryWarning { 160 | [super didReceiveMemoryWarning]; 161 | 162 | } 163 | 164 | - (void)dealloc { 165 | //NSLog(@"ablum dealloc"); 166 | } 167 | 168 | //MARK: - alertViewdelegate 169 | - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { 170 | if (buttonIndex == 1) { 171 | NSURL * url = [NSURL URLWithString:UIApplicationOpenSettingsURLString]; 172 | [[UIApplication sharedApplication] openURL:url]; 173 | } 174 | } 175 | 176 | @end 177 | -------------------------------------------------------------------------------- /YPhotoSelectDemo/选择照片/YPhotoSelect/YPhotoAblumTableViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.m 3 | // 选择照片 4 | // 5 | // Created by 彦鹏 on 16/7/20. 6 | // Copyright © 2016年 Huyp. All rights reserved. 7 | // 8 | 9 | #import "YPhotoAblumTableViewController.h" 10 | #import 11 | #import "YPhotothumbnailsViewController.h" 12 | 13 | @interface YPhotoAblumTableViewController () 14 | @property (strong,nonatomic) NSMutableArray * albums; 15 | @property (strong,nonatomic) NSMutableArray * thumbnails; 16 | @property (strong,nonatomic) NSMutableArray * names; 17 | @property (strong,nonatomic) NSMutableArray * amounts; 18 | @property (strong,nonatomic) ALAssetsLibrary * assetsLibrary; 19 | 20 | @end 21 | @implementation YPhotoAblumTableViewController 22 | 23 | 24 | 25 | - (UIStatusBarStyle)preferredStatusBarStyle { 26 | return UIStatusBarStyleLightContent; 27 | } 28 | 29 | - (void)cancel { 30 | [self dismissViewControllerAnimated:YES completion:nil]; 31 | } 32 | 33 | - (void)viewDidLoad { 34 | [super viewDidLoad]; 35 | 36 | self.title = @"相册"; 37 | self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(cancel)]; 38 | [self.navigationItem.rightBarButtonItem setTitleTextAttributes:@{ NSFontAttributeName : [UIFont systemFontOfSize:15]} forState:UIControlStateNormal]; 39 | 40 | UIBarButtonItem * backItem = [[UIBarButtonItem alloc]init]; 41 | backItem.title = @"相册"; 42 | [backItem setTitleTextAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:15]} forState:UIControlStateNormal]; 43 | self.navigationItem.backBarButtonItem = backItem; 44 | 45 | _albums = [[NSMutableArray alloc] init]; 46 | _thumbnails = [[NSMutableArray alloc] init]; 47 | _names = [[NSMutableArray alloc] init]; 48 | _amounts = [[NSMutableArray alloc] init]; 49 | 50 | self.tableView.delegate = self; 51 | self.tableView.dataSource = self; 52 | // self.tableView.separatorStyle = UITableViewCellSelectionStyleNone; 53 | 54 | [self loadAlbum]; 55 | } 56 | 57 | - (void)loadAlbum { 58 | // 获取当前应用对照片的访问授权状态 59 | ALAuthorizationStatus status = [ALAssetsLibrary authorizationStatus]; 60 | // 如果没有获取访问授权,或者访问授权状态已经被明确禁止,则显示提示语,引导用户开启授权 61 | if (status == ALAuthorizationStatusRestricted || status == ALAuthorizationStatusDenied) { 62 | UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"应用想访问您的相册" message:@"请设置应用的相册访问权限" delegate:self cancelButtonTitle:@"不允许" otherButtonTitles:@"去设置", nil]; 63 | [alert show]; 64 | } 65 | else { 66 | _assetsLibrary = [[ALAssetsLibrary alloc] init]; 67 | 68 | [_assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) { 69 | 70 | [group setAssetsFilter:[ALAssetsFilter allPhotos]]; 71 | if (group) { 72 | if (group.numberOfAssets > 0) { 73 | // 把相册储存到数组中,方便后面展示相册时使用 74 | [_albums addObject:group]; 75 | [_names addObject:[self getZH_CN:[group valueForProperty:ALAssetsGroupPropertyName]]]; 76 | [_amounts addObject:[NSString stringWithFormat:@"%ld",(long)[group numberOfAssets]]]; 77 | [_thumbnails addObject:[UIImage imageWithCGImage:[group posterImage]]]; 78 | } 79 | } 80 | else { 81 | [self.tableView reloadData]; 82 | } 83 | } 84 | failureBlock:^(NSError *error) { 85 | NSLog(@"Asset group not found!"); 86 | }]; 87 | } 88 | } 89 | 90 | - (NSString *)getZH_CN:(NSString *)name { 91 | if ([name isEqualToString:@"Favorites"]) { 92 | return @"个人收藏"; 93 | } 94 | else if ([name isEqualToString:@"Panoramas"]) { 95 | return @"全景照片"; 96 | } 97 | else if ([name isEqualToString:@"Camera Roll"]) { 98 | return @"相机胶卷"; 99 | } 100 | else if ([name isEqualToString:@"Screenshots"]) { 101 | return @"屏幕快照"; 102 | } 103 | else if ([name isEqualToString:@"Bursts"]) { 104 | return @"连拍快照"; 105 | } 106 | else if ([name isEqualToString:@"Selfies"]) { 107 | return @"自拍"; 108 | } 109 | else if ([name isEqualToString:@"Recently Added"]) { 110 | return @"最近添加"; 111 | } 112 | else { 113 | return name; 114 | } 115 | } 116 | 117 | - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 118 | return 1; 119 | } 120 | 121 | - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 122 | if (_albums == nil) { 123 | return 0; 124 | } 125 | else { 126 | return _albums.count; 127 | } 128 | } 129 | 130 | - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 131 | static NSString * cellID = @"album"; 132 | UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellID]; 133 | 134 | if (!cell) { 135 | cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID]; 136 | cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 137 | cell.imageView.contentMode = UIViewContentModeScaleAspectFill; 138 | } 139 | 140 | cell.imageView.image = _thumbnails[indexPath.row]; 141 | cell.textLabel.text = _names[indexPath.row]; 142 | cell.detailTextLabel.text = _amounts[indexPath.row]; 143 | 144 | return cell; 145 | } 146 | 147 | - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 148 | return [UIScreen mainScreen].bounds.size.height/9; 149 | } 150 | 151 | - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 152 | [tableView deselectRowAtIndexPath:indexPath animated:YES]; 153 | YPhotoThumbnailsViewController * photoThumbnailVC = [YPhotoThumbnailsViewController new]; 154 | photoThumbnailVC.album = _albums[indexPath.row]; 155 | photoThumbnailVC.lib = _assetsLibrary; 156 | [self.navigationController pushViewController:photoThumbnailVC animated:YES]; 157 | } 158 | 159 | - (void)didReceiveMemoryWarning { 160 | [super didReceiveMemoryWarning]; 161 | 162 | } 163 | 164 | - (void)dealloc { 165 | //NSLog(@"ablum dealloc"); 166 | } 167 | 168 | //MARK: - alertViewdelegate 169 | - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { 170 | if (buttonIndex == 1) { 171 | NSURL * url = [NSURL URLWithString:UIApplicationOpenSettingsURLString]; 172 | [[UIApplication sharedApplication] openURL:url]; 173 | } 174 | } 175 | 176 | @end 177 | -------------------------------------------------------------------------------- /YPhotoSelectDemo/YPhoto.storyboard: -------------------------------------------------------------------------------- 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 | -------------------------------------------------------------------------------- /YPhotoSelectDemo/选择照片/YPhotoPreviewViewController副本.m: -------------------------------------------------------------------------------- 1 | // 2 | // YPhotoPreviewViewController.m 3 | // 选择照片 4 | // 5 | // Created by 彦鹏 on 16/7/25. 6 | // Copyright © 2016年 Huyp. All rights reserved. 7 | // 8 | 9 | #import "YPhotoPreviewViewController.h" 10 | 11 | @interface YPhotoPreviewViewController () 12 | 13 | { 14 | BOOL isTwoImage; 15 | } 16 | 17 | @property (strong , nonatomic) UIImageView * imageViewA; //初始左边 18 | @property (strong , nonatomic) UIImageView * imageViewB; //初始中间 19 | @property (strong , nonatomic) UIImageView * imageViewC; //初始右边 20 | 21 | @property (strong , nonatomic) __block UIImage * leftImage; //初始左边 22 | @property (strong , nonatomic) __block UIImage * rightImage; //初始右边 23 | 24 | @end 25 | 26 | @implementation YPhotoPreviewViewController 27 | 28 | - (void)cancel { 29 | [self.navigationController popViewControllerAnimated:YES]; 30 | } 31 | - (void)selectOrDisSelect { 32 | NSLog(@"select"); 33 | } 34 | 35 | - (void)setNavgation { 36 | self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"<" style:UIBarButtonItemStylePlain target:self action:@selector(cancel)]; 37 | self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"YPhoto.bundle/y_icon_image_no"] style:UIBarButtonItemStylePlain target:self action:@selector(selectOrDisSelect)]; 38 | } 39 | 40 | - (void)setToolBar { 41 | UIToolbar * toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height - 44, [UIScreen mainScreen].bounds.size.width, 44)]; 42 | 43 | UIBarButtonItem * flexibleBarBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil]; 44 | UIBarButtonItem * rightItem = [[UIBarButtonItem alloc]initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(done)]; 45 | [rightItem setTintColor:[UIColor greenColor]]; 46 | toolBar.items = @[flexibleBarBtn,rightItem]; 47 | 48 | [self.view addSubview:toolBar]; 49 | } 50 | 51 | - (void)setScrollView { 52 | ALAuthorizationStatus status = [ALAssetsLibrary authorizationStatus]; 53 | // 如果没有获取访问授权,或者访问授权状态已经被明确禁止,则显示提示语,引导用户开启授权 54 | if (status == ALAuthorizationStatusRestricted || status == ALAuthorizationStatusDenied) { 55 | UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"应用想访问您的相册" message:@"请设置应用的相册访问权限" delegate:self cancelButtonTitle:@"不允许" otherButtonTitles:@"去设置", nil]; 56 | [alert show]; 57 | } 58 | else { 59 | UIScrollView * scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(-3, 0, [UIScreen mainScreen].bounds.size.width + 6, [UIScreen mainScreen].bounds.size.height)]; 60 | scrollView.delegate = self; 61 | [self.view addSubview:scrollView]; 62 | scrollView.backgroundColor = [UIColor blackColor]; 63 | scrollView.pagingEnabled = YES; 64 | scrollView.showsHorizontalScrollIndicator = NO; 65 | scrollView.showsVerticalScrollIndicator = NO; 66 | scrollView.alwaysBounceVertical = NO; 67 | scrollView.alwaysBounceHorizontal = YES; 68 | scrollView.contentMode = UIViewContentModeCenter; 69 | scrollView.contentSize = CGSizeMake(([UIScreen mainScreen].bounds.size.width + 6) * _ALAssets.count, 0); 70 | scrollView.contentOffset = CGPointMake(([UIScreen mainScreen].bounds.size.width + 6) * _indexPath.row, 0); 71 | 72 | _imageViewA = [self showThumbnailImageViewAtIndexPath:_indexPath.row - 1]; 73 | _imageViewB = [self showThumbnailImageViewAtIndexPath:_indexPath.row]; 74 | _imageViewC = [self showThumbnailImageViewAtIndexPath:_indexPath.row + 1]; 75 | 76 | [scrollView addSubview:_imageViewA]; 77 | [scrollView addSubview:_imageViewB]; 78 | [scrollView addSubview:_imageViewC]; 79 | 80 | //异步获取fullscreen图片 81 | dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 82 | ALAssetRepresentation * representation = [_ALAssets[_indexPath.row] defaultRepresentation]; 83 | UIImage * image = [UIImage imageWithCGImage:[representation fullResolutionImage]] ; 84 | dispatch_async(dispatch_get_main_queue(), ^{ 85 | _imageViewB.image = image; 86 | NSLog(@"scrollView=%@",scrollView.subviews); 87 | }); 88 | }); 89 | } 90 | } 91 | 92 | - (void)viewDidLoad { 93 | [super viewDidLoad]; 94 | 95 | [self setNavgation]; 96 | 97 | [self setScrollView]; 98 | 99 | [self setToolBar]; 100 | } 101 | 102 | - (UIImageView *)showThumbnailImageViewAtIndexPath:(NSInteger)indexPathRow{ 103 | 104 | if ((indexPathRow < 0)||(indexPathRow > _ALAssets.count - 1)) { 105 | return nil; 106 | } 107 | 108 | UIImageView * imageView = [[UIImageView alloc]initWithImage:[self thumbnailImage:indexPathRow]]; 109 | imageView.contentMode = UIViewContentModeScaleAspectFit; 110 | imageView.frame = CGRectMake(3 + ([UIScreen mainScreen].bounds.size.width + 6) * indexPathRow, -64, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height); 111 | return imageView; 112 | } 113 | 114 | - (UIImage *)thumbnailImage:(NSInteger)indexPathRow { 115 | if ((indexPathRow < 0)||(indexPathRow > _ALAssets.count-1)) { 116 | return nil; 117 | } 118 | return [UIImage imageWithCGImage:[_ALAssets[indexPathRow] aspectRatioThumbnail]]; 119 | } 120 | 121 | - (UIImageView *)showFullscreenImageViewAtIndexPath:(NSInteger)indexPathRow{ 122 | 123 | if ((indexPathRow < 0)||(indexPathRow > _ALAssets.count)) { 124 | return nil; 125 | } 126 | 127 | UIImageView * imageView = [[UIImageView alloc]initWithImage:[self fullscreenImage:indexPathRow]]; 128 | imageView.contentMode = UIViewContentModeScaleAspectFit; 129 | imageView.frame = CGRectMake(3 + ([UIScreen mainScreen].bounds.size.width + 6) * indexPathRow, -64, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height); 130 | return imageView; 131 | } 132 | 133 | - (UIImage *)fullscreenImage :(NSInteger)indexPathRow { 134 | if ((indexPathRow < 0)||(indexPathRow > _ALAssets.count)) { 135 | return nil; 136 | } 137 | ALAssetRepresentation * representation = [_ALAssets[indexPathRow] defaultRepresentation]; 138 | return [UIImage imageWithCGImage:[representation fullResolutionImage]]; 139 | } 140 | 141 | - (void)done { 142 | NSLog(@"done"); 143 | } 144 | 145 | - (void)didReceiveMemoryWarning { 146 | [super didReceiveMemoryWarning]; 147 | // Dispose of any resources that can be recreated. 148 | } 149 | 150 | - (void)scrollViewDidScroll:(UIScrollView *)scrollView { 151 | 152 | dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 153 | //获取要显示的图片的index 过半算 154 | int indexPathRow = (int)roundf(scrollView.contentOffset.x/([UIScreen mainScreen].bounds.size.width+6)); 155 | 156 | //向右划 显示左边的图片 157 | if (indexPathRow == _indexPath.row) { 158 | return ; 159 | } 160 | else if (indexPathRow < _indexPath.row) { 161 | _indexPath = [NSIndexPath indexPathForRow:indexPathRow inSection:1]; 162 | if (scrollView.subviews.count == 1) { 163 | return; 164 | } 165 | else if (scrollView.subviews.count == 2) { 166 | if (_ALAssets.count == 2) { //相册只有2张图片 模糊转走这张 167 | return; 168 | } 169 | else { //因为是右滑 所以当前肯定是最后一张图片 170 | UIImageView * leftImageView = [self showThumbnailImageViewAtIndexPath:indexPathRow-1]; 171 | dispatch_async(dispatch_get_main_queue(), ^{ 172 | [scrollView insertSubview:leftImageView atIndex:0]; 173 | }); 174 | } 175 | } 176 | else { 177 | if (indexPathRow == 0) { //移动到了第一张 178 | [scrollView.subviews.lastObject removeFromSuperview]; 179 | } 180 | else { 181 | ((UIImageView *)scrollView.subviews.lastObject).image = [self thumbnailImage:indexPathRow-1]; 182 | dispatch_async(dispatch_get_main_queue(), ^{ 183 | NSLog(@"1"); 184 | (scrollView.subviews.lastObject).frame = CGRectMake(3 + ([UIScreen mainScreen].bounds.size.width + 6) * (indexPathRow-1), -64, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height); 185 | [scrollView insertSubview:scrollView.subviews.lastObject atIndex:0]; 186 | }); 187 | } 188 | } 189 | } 190 | else { //左滑 显示右边的图片 191 | _indexPath = [NSIndexPath indexPathForRow:indexPathRow inSection:1]; 192 | if (scrollView.subviews.count == 1) { 193 | return; 194 | } 195 | else if (scrollView.subviews.count == 2) { 196 | if (_ALAssets.count == 2) { //相册只有2张图片 模糊转走这张 197 | return; 198 | } 199 | else { //因为是左滑 所以当前肯定是第一张图片 200 | UIImageView * rightImageView = [self showThumbnailImageViewAtIndexPath:indexPathRow+1]; 201 | dispatch_async(dispatch_get_main_queue(), ^{ 202 | [scrollView insertSubview:rightImageView atIndex:1]; 203 | }); 204 | } 205 | } 206 | else { 207 | if (indexPathRow == _ALAssets.count-1) { //移动到了最后一张 208 | [scrollView.subviews.firstObject removeFromSuperview]; 209 | } 210 | else { 211 | ((UIImageView *)scrollView.subviews.firstObject).image = [self thumbnailImage:indexPathRow+1]; 212 | dispatch_async(dispatch_get_main_queue(), ^{ 213 | NSLog(@"1"); 214 | (scrollView.subviews.firstObject).frame = CGRectMake(3 + ([UIScreen mainScreen].bounds.size.width + 6) * (indexPathRow-1), -64, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height); 215 | [scrollView insertSubview:scrollView.subviews.firstObject atIndex:2]; 216 | }); 217 | } 218 | } 219 | } 220 | }); 221 | } 222 | 223 | - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { 224 | //获取要显示的图片的index 过半算 225 | int indexPathRow = (int)roundf(scrollView.contentOffset.x/([UIScreen mainScreen].bounds.size.width+6)); 226 | 227 | NSLog(@"indexPathRow=%d",indexPathRow); 228 | dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 229 | if (scrollView.subviews.count == 2) { 230 | 231 | if (isTwoImage == NO && _ALAssets.count == 2) { 232 | UIImage * leftImage = [self fullscreenImage:0]; 233 | UIImage * rightImage = [self fullscreenImage:1]; 234 | dispatch_async(dispatch_get_main_queue(), ^{ 235 | ((UIImageView *)scrollView.subviews[0]).image = leftImage; 236 | ((UIImageView *)scrollView.subviews[1]).image = rightImage; 237 | isTwoImage = YES; 238 | }); 239 | } 240 | else if (indexPathRow == 0) { 241 | UIImage * leftImage = [self fullscreenImage:indexPathRow]; 242 | // UIImage * rightImage = [self thumbnailImage:indexPathRow + 1]; 243 | dispatch_async(dispatch_get_main_queue(), ^{ 244 | ((UIImageView *)scrollView.subviews[0]).image = leftImage; 245 | // ((UIImageView *)scrollView.subviews[1]).image = rightImage; 246 | }); 247 | } 248 | else if (indexPathRow == _ALAssets.count-1) { 249 | // UIImage * leftImage = [self thumbnailImage:indexPathRow-1]; 250 | UIImage * rightImage = [self fullscreenImage:indexPathRow]; 251 | dispatch_async(dispatch_get_main_queue(), ^{ 252 | // ((UIImageView *)scrollView.subviews[0]).image = leftImage; 253 | ((UIImageView *)scrollView.subviews[1]).image = rightImage; 254 | }); 255 | } 256 | } 257 | else if(scrollView.subviews.count == 3){ 258 | _centerImage = [self fullscreenImage:indexPathRow]; 259 | // _leftImage = [self thumbnailImage:indexPathRow - 1]; 260 | // _rightImage = [self thumbnailImage:indexPathRow +1]; 261 | dispatch_async(dispatch_get_main_queue(), ^{ 262 | // ((UIImageView *)scrollView.subviews[0]).image = _leftImage; 263 | ((UIImageView *)scrollView.subviews[1]).image = _centerImage; 264 | // ((UIImageView *)scrollView.subviews[2]).image = _rightImage; 265 | }); 266 | } 267 | }); 268 | } 269 | 270 | 271 | 272 | //MARK: - alertViewdelegate 273 | - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { 274 | if (buttonIndex == 1) { 275 | NSURL * url = [NSURL URLWithString:UIApplicationOpenSettingsURLString]; 276 | [[UIApplication sharedApplication] openURL:url]; 277 | } 278 | } 279 | 280 | - (void)dealloc { 281 | NSLog(@"dealloc"); 282 | } 283 | @end 284 | -------------------------------------------------------------------------------- /YPhotoSelect/YPhotoThumbnailsViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // YPhotothumbnailsViewController.m 3 | // 选择照片 4 | // 5 | // Created by 彦鹏 on 16/7/20. 6 | // Copyright © 2016年 Huyp. All rights reserved. 7 | // 8 | 9 | #import "YPhotoThumbnailsViewController.h" 10 | #import "YPhotoPreviewViewController.h" 11 | #import "YPhotoGlobalVar.h" 12 | 13 | #import "YPhotoAblumTableViewController.h" 14 | 15 | @interface YPhotoThumbnailsViewController () 16 | @property (strong,nonatomic) UICollectionView * collectionView; 17 | @property (strong,nonatomic) UIToolbar * toolBar; 18 | @property (assign,nonatomic) BOOL isMaxNum; 19 | @property (strong,nonatomic) NSMutableDictionary * selectedFullScreenImages; 20 | @property (strong,nonatomic) NSMutableArray * aLAssets; 21 | @property (strong,nonatomic) UIBarButtonItem * selectBtn; 22 | @property (strong,nonatomic) UILabel * selectNumLabel; 23 | @property (strong,nonatomic) ALAssetRepresentation * representation; 24 | @property (strong,nonatomic) UIBarButtonItem * previewBtn; 25 | @property (strong,nonatomic) YPhotoGlobalVar * globalVar; 26 | @end 27 | 28 | @implementation YPhotoThumbnailsViewController 29 | 30 | - (UIStatusBarStyle)preferredStatusBarStyle { 31 | return UIStatusBarStyleLightContent; 32 | } 33 | 34 | - (NSString *)getZH_CN:(NSString *)name { 35 | if ([name isEqualToString:@"Favorites"]) { 36 | return @"个人收藏"; 37 | } 38 | else if ([name isEqualToString:@"Panoramas"]) { 39 | return @"全景照片"; 40 | } 41 | else if ([name isEqualToString:@"Camera Roll"]) { 42 | return @"相机胶卷"; 43 | } 44 | else if ([name isEqualToString:@"Screenshots"]) { 45 | return @"屏幕快照"; 46 | } 47 | else if ([name isEqualToString:@"Bursts"]) { 48 | return @"连拍快照"; 49 | } 50 | else if ([name isEqualToString:@"Selfies"]) { 51 | return @"自拍"; 52 | } 53 | else if ([name isEqualToString:@"Recently Added"]) { 54 | return @"最近添加"; 55 | } 56 | else { 57 | return name; 58 | } 59 | } 60 | 61 | - (void)back { 62 | [self.navigationController popViewControllerAnimated:YES]; 63 | } 64 | 65 | - (void)cancel { 66 | _globalVar.currentNum = 0; 67 | _globalVar.indexPaths = nil; 68 | _globalVar.selectedThumbnails = nil; 69 | _globalVar.selectedImgs = nil; 70 | _globalVar.selectedAlassets = nil; 71 | _globalVar.reloadIndexPaths = nil; 72 | _globalVar.animation = nil; 73 | 74 | [self dismissViewControllerAnimated:YES completion:nil]; 75 | } 76 | 77 | - (void)loadNavgationBar { 78 | UIBarButtonItem * backItem = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(cancel)]; 79 | [backItem setTitleTextAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:15]} forState:UIControlStateNormal]; 80 | self.navigationItem.backBarButtonItem = backItem; 81 | 82 | self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(cancel)]; 83 | [self.navigationItem.rightBarButtonItem setTitleTextAttributes:@{ NSFontAttributeName : [UIFont systemFontOfSize:15]} forState:UIControlStateNormal]; 84 | 85 | self.title = [self getZH_CN:[_album valueForProperty:ALAssetsGroupPropertyName]]; 86 | } 87 | 88 | - (UIImage*)createImageWithColor:(UIColor*) color 89 | { 90 | CGRect rect=CGRectMake(0.0f, 0.0f, 1.0f, 1.0f); 91 | UIGraphicsBeginImageContext(rect.size); 92 | CGContextRef context = UIGraphicsGetCurrentContext(); 93 | CGContextSetFillColorWithColor(context, [color CGColor]); 94 | CGContextFillRect(context, rect); 95 | UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext(); 96 | UIGraphicsEndImageContext(); 97 | return theImage; 98 | } 99 | 100 | - (void)setToolBar { 101 | _toolBar = [UIToolbar new]; 102 | 103 | _toolBar.translatesAutoresizingMaskIntoConstraints = NO; 104 | UIImage * bgImage = [self createImageWithColor:[UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0]]; 105 | [_toolBar setBackgroundImage:bgImage forToolbarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault]; 106 | [self.view addSubview:_toolBar]; 107 | 108 | NSArray * h = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[toolBar]-0-|" options:NSLayoutFormatDirectionLeadingToTrailing metrics:nil views:@{@"toolBar":_toolBar}]; 109 | //NSArray * v = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[toolBar(44)]-0-|" options:NSLayoutFormatDirectionLeadingToTrailing metrics:nil views:@{@"toolBar":_toolBar}]; 110 | 111 | _previewBtn = [[UIBarButtonItem alloc]initWithTitle:@"预览" style:UIBarButtonItemStylePlain target:self action:@selector(presentTap)]; 112 | [_previewBtn setTintColor:[UIColor grayColor]]; 113 | _previewBtn.enabled = NO; 114 | 115 | UIBarButtonItem * flexibleBarBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil]; 116 | 117 | _selectBtn = [[UIBarButtonItem alloc]initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(doneTap)]; 118 | [_selectBtn setTintColor:[UIColor greenColor]]; 119 | _selectBtn.enabled = NO; 120 | 121 | _selectNumLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 20, 20)]; 122 | _selectNumLabel.layer.masksToBounds = YES; 123 | _selectNumLabel.layer.cornerRadius = 10; 124 | _selectNumLabel.backgroundColor = [UIColor greenColor]; 125 | _selectNumLabel.textAlignment = NSTextAlignmentCenter; 126 | _selectNumLabel.textColor = [UIColor whiteColor]; 127 | 128 | UIBarButtonItem * label = [[UIBarButtonItem alloc]initWithCustomView:_selectNumLabel]; 129 | 130 | _toolBar.items = @[_previewBtn,flexibleBarBtn,label,_selectBtn]; 131 | 132 | [self.view addConstraints:h]; 133 | //[self.view addConstraints:v]; 134 | 135 | NSArray * toolBar_collention = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[collectionView]-0-[toolBar(44)]-0-|" options:NSLayoutFormatDirectionLeadingToTrailing metrics:nil views:@{@"collectionView":_collectionView,@"toolBar":_toolBar}]; 136 | [self.view addConstraints:toolBar_collention]; 137 | } 138 | 139 | 140 | - (void)loadAlAsset { 141 | _aLAssets = [NSMutableArray array]; 142 | // 获取当前应用对照片的访问授权状态 143 | ALAuthorizationStatus status = [ALAssetsLibrary authorizationStatus]; 144 | // 如果没有获取访问授权,或者访问授权状态已经被明确禁止,则显示提示语,引导用户开启授权 145 | if (status == ALAuthorizationStatusRestricted || status == ALAuthorizationStatusDenied) { 146 | UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"应用想访问您的相册" message:@"请设置应用的相册访问权限" delegate:self cancelButtonTitle:@"不允许" otherButtonTitles:@"去设置", nil]; 147 | [alert show]; 148 | } 149 | else { 150 | [_album enumerateAssetsWithOptions:NSEnumerationReverse usingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) { 151 | if (result) { 152 | [_aLAssets addObject:result]; 153 | } 154 | }]; 155 | } 156 | } 157 | 158 | - (void)loadCollectionView { 159 | 160 | UICollectionViewFlowLayout * pickImageCollectionLayout = [UICollectionViewFlowLayout new]; 161 | pickImageCollectionLayout.scrollDirection = UICollectionViewScrollDirectionVertical; 162 | pickImageCollectionLayout.minimumInteritemSpacing = 2; 163 | pickImageCollectionLayout.minimumLineSpacing = 2; 164 | _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 44) collectionViewLayout:pickImageCollectionLayout]; 165 | _collectionView.backgroundColor = [UIColor whiteColor]; 166 | _collectionView.delegate = self; 167 | _collectionView.dataSource = self; 168 | [_collectionView registerNib:[UINib nibWithNibName:@"YPhotoCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"YPhotoCollectionCellID"]; 169 | [self.view addSubview:_collectionView]; 170 | } 171 | 172 | - (void)viewDidLoad { 173 | [super viewDidLoad]; 174 | 175 | _globalVar = [YPhotoGlobalVar shareGlobalVar]; 176 | 177 | [self loadNavgationBar]; 178 | [self loadAlAsset]; 179 | [self loadCollectionView]; 180 | [self setToolBar]; 181 | } 182 | 183 | - (void)viewDidAppear:(BOOL)animated { 184 | [super viewDidAppear:animated]; 185 | 186 | self.navigationController.interactivePopGestureRecognizer.enabled = YES; 187 | 188 | //NSLog(@"_globalVar.indexPaths=%@",_globalVar.indexPaths); 189 | if ( _globalVar.reloadIndexPaths.count > 0) { 190 | [_collectionView reloadItemsAtIndexPaths:_globalVar.reloadIndexPaths]; 191 | } 192 | } 193 | 194 | - (void)viewWillAppear:(BOOL)animated { 195 | [super viewWillAppear:animated]; 196 | 197 | if (_globalVar.selectedAlassets.count > 0) { 198 | _previewBtn.enabled = YES; 199 | _selectBtn.enabled = YES; 200 | 201 | _selectNumLabel.hidden = NO; 202 | _selectNumLabel.text = [NSString stringWithFormat:@"%d",_globalVar.currentNum]; 203 | } else { 204 | _previewBtn.enabled = NO; 205 | _selectBtn.enabled = NO; 206 | 207 | _selectNumLabel.hidden = YES; 208 | } 209 | } 210 | 211 | - (void)presentTap { 212 | if (_globalVar.currentNum <= 0) { 213 | return; 214 | } 215 | 216 | UIImage * centerImage = [UIImage imageWithCGImage:[_globalVar.selectedAlassets[0] thumbnail]] ; 217 | YPhotoPreviewViewController * previewVC = [YPhotoPreviewViewController new]; 218 | previewVC.centerImage = centerImage; 219 | NSIndexPath * indepath = [NSIndexPath indexPathForRow:0 inSection:1]; 220 | previewVC.indexPath = indepath; 221 | previewVC.aLAssets = _globalVar.selectedAlassets; 222 | previewVC.lib = _lib; 223 | previewVC.isPresent = YES; 224 | [self.navigationController pushViewController:previewVC animated:YES]; 225 | } 226 | 227 | - (void)doneTap { 228 | 229 | UIActivityIndicatorView * activityIV = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]; 230 | activityIV.center = CGPointMake([UIScreen mainScreen].bounds.size.width/2, [UIScreen mainScreen].bounds.size.height/3); 231 | [self.view addSubview:activityIV]; 232 | [activityIV startAnimating]; 233 | 234 | UIImage * fullScreenImg; 235 | 236 | for (int i = 0; i < _globalVar.selectedAlassets.count; i++) { 237 | _representation = [_globalVar.selectedAlassets[i] defaultRepresentation]; 238 | 239 | fullScreenImg = [UIImage imageWithCGImage:[_representation fullScreenImage]] ; 240 | fullScreenImg = [self compressionImage:fullScreenImg WithScale:_globalVar.targetImageScale]; 241 | 242 | [_globalVar.selectedImgs addObject:fullScreenImg]; 243 | 244 | UIImage * thumbnail = [UIImage imageWithCGImage:((ALAsset *)_globalVar.selectedAlassets[i]).thumbnail]; 245 | [_globalVar.selectedThumbnails addObject:thumbnail]; 246 | } 247 | 248 | _globalVar.indexPaths = nil; 249 | _globalVar.reloadIndexPaths = nil; 250 | _globalVar.selectedAlassets = nil; 251 | _globalVar.animation = nil; 252 | 253 | [activityIV stopAnimating]; 254 | [activityIV removeFromSuperview]; 255 | 256 | [self dismissViewControllerAnimated:YES completion:nil]; 257 | 258 | } 259 | 260 | - (UIImage *)compressionImage:(UIImage *)image WithScale:(CGFloat)scale { 261 | /* 按给定尺寸 按比例缩放的方法 262 | 实现等比例缩放 263 | double hfactor = image.size.width / targetSize.width; 264 | double vfactor = image.size.height / targetSize.height; 265 | double factor = fmax(hfactor, vfactor); 266 | 绘制画布 267 | double newWith = image.size.width / factor; 268 | double newHeight = image.size.height / factor; 269 | CGSize newSize = CGSizeMake(newWith, newHeight); 270 | */ 271 | /* 272 | 按照给定缩放比例缩放 273 | */ 274 | double newWith = image.size.width * scale; 275 | double newHeight = image.size.height * scale; 276 | CGSize newSize = CGSizeMake(newWith, newHeight); 277 | 278 | UIGraphicsBeginImageContext(newSize); 279 | [image drawInRect:CGRectMake(0, 0, newWith, newHeight)]; 280 | UIImage * newImage = UIGraphicsGetImageFromCurrentImageContext(); 281 | UIGraphicsEndImageContext(); 282 | 283 | //按照jpg格式进行了压缩,如果需要png格式就把这个换成 : UIImagePNGRepresentation(newImage); 284 | NSData * data; 285 | if (_globalVar.png == YES) { 286 | data = UIImagePNGRepresentation(newImage); 287 | } 288 | else { 289 | data = UIImageJPEGRepresentation(newImage, 0.5);//二次压缩 290 | } 291 | newImage = [UIImage imageWithData:data]; 292 | 293 | return newImage; 294 | } 295 | 296 | - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { 297 | return 1; 298 | } 299 | 300 | - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { 301 | return _aLAssets.count; 302 | } 303 | - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { 304 | 305 | static NSString * cellID = @"YPhotoCollectionCellID"; 306 | __weak YPhotoCollectionViewCell * cell = (YPhotoCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath]; 307 | if ([_globalVar.indexPaths containsObject:indexPath]) { 308 | cell.isPicked = YES; 309 | [cell.button setImage:_globalVar.yesImg forState:UIControlStateNormal]; 310 | } else { 311 | cell.isPicked = NO; 312 | [cell.button setImage:_globalVar.noImg forState:UIControlStateNormal]; 313 | } 314 | 315 | cell.selectBlock = ^(BOOL isSelect) { 316 | if (isSelect == YES) { 317 | 318 | _globalVar.currentNum ++; 319 | if (_globalVar.currentNum > 0) { 320 | _previewBtn.enabled = YES; 321 | _selectBtn.enabled = YES; 322 | 323 | _selectNumLabel.hidden = NO; 324 | } 325 | _selectNumLabel.text = [NSString stringWithFormat:@"%d",_globalVar.currentNum]; 326 | [self selectCompressionImageWithIndexPath:(NSIndexPath *)indexPath]; 327 | } 328 | else if (isSelect == NO) { 329 | _globalVar.currentNum --; 330 | if (_globalVar.currentNum == 0) { 331 | _previewBtn.enabled = NO; 332 | _selectBtn.enabled = NO; 333 | 334 | _selectNumLabel.hidden = YES; 335 | } 336 | if (_globalVar.currentNum >= 0) { 337 | _selectNumLabel.text = [NSString stringWithFormat:@"%d",_globalVar.currentNum]; 338 | [self deleteCompressionImageIndexPath:(NSIndexPath *)indexPath]; 339 | } 340 | } 341 | }; 342 | cell.imageView.image = [UIImage imageWithCGImage:((ALAsset *)_aLAssets[indexPath.row]).thumbnail]; 343 | 344 | return cell; 345 | } 346 | 347 | - (void)selectCompressionImageWithIndexPath:(NSIndexPath *)indexPath { 348 | 349 | [_globalVar.selectedAlassets addObject:_aLAssets[indexPath.row]]; 350 | [_globalVar.indexPaths addObject:indexPath]; 351 | } 352 | 353 | - (void)deleteCompressionImageIndexPath:(NSIndexPath *)indexPath { 354 | [_globalVar.selectedAlassets removeObject:_aLAssets[indexPath.row]]; 355 | [_globalVar.indexPaths removeObject:indexPath]; 356 | } 357 | 358 | - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { 359 | 360 | UIImage * centerImage = [UIImage imageWithCGImage:((ALAsset *)_aLAssets[indexPath.row]).thumbnail]; 361 | YPhotoPreviewViewController * previewVC = [YPhotoPreviewViewController new]; 362 | previewVC.centerImage = centerImage; 363 | previewVC.indexPath = indexPath; 364 | previewVC.aLAssets = _aLAssets; 365 | previewVC.lib = _lib; 366 | [self.navigationController pushViewController:previewVC animated:YES]; 367 | } 368 | 369 | - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section { 370 | return UIEdgeInsetsMake(2, 2, 2, 2); 371 | } 372 | 373 | - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { 374 | CGFloat cellWH = ([UIScreen mainScreen].bounds.size.width - 10) / 4; 375 | CGSize cellSize = CGSizeMake(cellWH, cellWH); 376 | return cellSize; 377 | } 378 | 379 | //MARK: - alertViewdelegate 380 | - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { 381 | if (buttonIndex == 1) { 382 | NSURL * url = [NSURL URLWithString:UIApplicationOpenSettingsURLString]; 383 | [[UIApplication sharedApplication] openURL:url]; 384 | } 385 | } 386 | 387 | - (void)didReceiveMemoryWarning { 388 | [super didReceiveMemoryWarning]; 389 | // Dispose of any resources that can be recreated. 390 | } 391 | 392 | -(void)dealloc { 393 | //NSLog(@"thumbnail dealloc"); 394 | } 395 | 396 | @end 397 | -------------------------------------------------------------------------------- /YPhotoSelectDemo/选择照片/YPhotoSelect/YPhotoThumbnailsViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // YPhotothumbnailsViewController.m 3 | // 选择照片 4 | // 5 | // Created by 彦鹏 on 16/7/20. 6 | // Copyright © 2016年 Huyp. All rights reserved. 7 | // 8 | 9 | #import "YPhotoThumbnailsViewController.h" 10 | #import "YPhotoPreviewViewController.h" 11 | #import "YPhotoGlobalVar.h" 12 | 13 | #import "YPhotoAblumTableViewController.h" 14 | 15 | @interface YPhotoThumbnailsViewController () 16 | @property (strong,nonatomic) UICollectionView * collectionView; 17 | @property (strong,nonatomic) UIToolbar * toolBar; 18 | @property (assign,nonatomic) BOOL isMaxNum; 19 | @property (strong,nonatomic) NSMutableDictionary * selectedFullScreenImages; 20 | @property (strong,nonatomic) NSMutableArray * aLAssets; 21 | @property (strong,nonatomic) UIBarButtonItem * selectBtn; 22 | @property (strong,nonatomic) UILabel * selectNumLabel; 23 | @property (strong,nonatomic) ALAssetRepresentation * representation; 24 | @property (strong,nonatomic) UIBarButtonItem * previewBtn; 25 | @property (strong,nonatomic) YPhotoGlobalVar * globalVar; 26 | @end 27 | 28 | @implementation YPhotoThumbnailsViewController 29 | 30 | - (UIStatusBarStyle)preferredStatusBarStyle { 31 | return UIStatusBarStyleLightContent; 32 | } 33 | 34 | - (NSString *)getZH_CN:(NSString *)name { 35 | if ([name isEqualToString:@"Favorites"]) { 36 | return @"个人收藏"; 37 | } 38 | else if ([name isEqualToString:@"Panoramas"]) { 39 | return @"全景照片"; 40 | } 41 | else if ([name isEqualToString:@"Camera Roll"]) { 42 | return @"相机胶卷"; 43 | } 44 | else if ([name isEqualToString:@"Screenshots"]) { 45 | return @"屏幕快照"; 46 | } 47 | else if ([name isEqualToString:@"Bursts"]) { 48 | return @"连拍快照"; 49 | } 50 | else if ([name isEqualToString:@"Selfies"]) { 51 | return @"自拍"; 52 | } 53 | else if ([name isEqualToString:@"Recently Added"]) { 54 | return @"最近添加"; 55 | } 56 | else { 57 | return name; 58 | } 59 | } 60 | 61 | - (void)back { 62 | [self.navigationController popViewControllerAnimated:YES]; 63 | } 64 | 65 | - (void)cancel { 66 | _globalVar.currentNum = 0; 67 | _globalVar.indexPaths = nil; 68 | _globalVar.selectedThumbnails = nil; 69 | _globalVar.selectedImgs = nil; 70 | _globalVar.selectedAlassets = nil; 71 | _globalVar.reloadIndexPaths = nil; 72 | _globalVar.animation = nil; 73 | 74 | [self dismissViewControllerAnimated:YES completion:nil]; 75 | } 76 | 77 | - (void)loadNavgationBar { 78 | UIBarButtonItem * backItem = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(cancel)]; 79 | [backItem setTitleTextAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:15]} forState:UIControlStateNormal]; 80 | self.navigationItem.backBarButtonItem = backItem; 81 | 82 | self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(cancel)]; 83 | [self.navigationItem.rightBarButtonItem setTitleTextAttributes:@{ NSFontAttributeName : [UIFont systemFontOfSize:15]} forState:UIControlStateNormal]; 84 | 85 | self.title = [self getZH_CN:[_album valueForProperty:ALAssetsGroupPropertyName]]; 86 | } 87 | 88 | - (UIImage*)createImageWithColor:(UIColor*) color 89 | { 90 | CGRect rect=CGRectMake(0.0f, 0.0f, 1.0f, 1.0f); 91 | UIGraphicsBeginImageContext(rect.size); 92 | CGContextRef context = UIGraphicsGetCurrentContext(); 93 | CGContextSetFillColorWithColor(context, [color CGColor]); 94 | CGContextFillRect(context, rect); 95 | UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext(); 96 | UIGraphicsEndImageContext(); 97 | return theImage; 98 | } 99 | 100 | - (void)setToolBar { 101 | _toolBar = [UIToolbar new]; 102 | 103 | _toolBar.translatesAutoresizingMaskIntoConstraints = NO; 104 | UIImage * bgImage = [self createImageWithColor:[UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0]]; 105 | [_toolBar setBackgroundImage:bgImage forToolbarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault]; 106 | [self.view addSubview:_toolBar]; 107 | 108 | NSArray * h = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[toolBar]-0-|" options:NSLayoutFormatDirectionLeadingToTrailing metrics:nil views:@{@"toolBar":_toolBar}]; 109 | //NSArray * v = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[toolBar(44)]-0-|" options:NSLayoutFormatDirectionLeadingToTrailing metrics:nil views:@{@"toolBar":_toolBar}]; 110 | 111 | _previewBtn = [[UIBarButtonItem alloc]initWithTitle:@"预览" style:UIBarButtonItemStylePlain target:self action:@selector(presentTap)]; 112 | [_previewBtn setTintColor:[UIColor grayColor]]; 113 | _previewBtn.enabled = NO; 114 | 115 | UIBarButtonItem * flexibleBarBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil]; 116 | 117 | _selectBtn = [[UIBarButtonItem alloc]initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(doneTap)]; 118 | [_selectBtn setTintColor:[UIColor greenColor]]; 119 | _selectBtn.enabled = NO; 120 | 121 | _selectNumLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 20, 20)]; 122 | _selectNumLabel.layer.masksToBounds = YES; 123 | _selectNumLabel.layer.cornerRadius = 10; 124 | _selectNumLabel.backgroundColor = [UIColor greenColor]; 125 | _selectNumLabel.textAlignment = NSTextAlignmentCenter; 126 | _selectNumLabel.textColor = [UIColor whiteColor]; 127 | 128 | UIBarButtonItem * label = [[UIBarButtonItem alloc]initWithCustomView:_selectNumLabel]; 129 | 130 | _toolBar.items = @[_previewBtn,flexibleBarBtn,label,_selectBtn]; 131 | 132 | [self.view addConstraints:h]; 133 | //[self.view addConstraints:v]; 134 | 135 | NSArray * toolBar_collention = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[collectionView]-0-[toolBar(44)]-0-|" options:NSLayoutFormatDirectionLeadingToTrailing metrics:nil views:@{@"collectionView":_collectionView,@"toolBar":_toolBar}]; 136 | [self.view addConstraints:toolBar_collention]; 137 | } 138 | 139 | 140 | - (void)loadAlAsset { 141 | _aLAssets = [NSMutableArray array]; 142 | // 获取当前应用对照片的访问授权状态 143 | ALAuthorizationStatus status = [ALAssetsLibrary authorizationStatus]; 144 | // 如果没有获取访问授权,或者访问授权状态已经被明确禁止,则显示提示语,引导用户开启授权 145 | if (status == ALAuthorizationStatusRestricted || status == ALAuthorizationStatusDenied) { 146 | UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"应用想访问您的相册" message:@"请设置应用的相册访问权限" delegate:self cancelButtonTitle:@"不允许" otherButtonTitles:@"去设置", nil]; 147 | [alert show]; 148 | } 149 | else { 150 | [_album enumerateAssetsWithOptions:NSEnumerationReverse usingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) { 151 | if (result) { 152 | [_aLAssets addObject:result]; 153 | } 154 | }]; 155 | } 156 | } 157 | 158 | - (void)loadCollectionView { 159 | 160 | UICollectionViewFlowLayout * pickImageCollectionLayout = [UICollectionViewFlowLayout new]; 161 | pickImageCollectionLayout.scrollDirection = UICollectionViewScrollDirectionVertical; 162 | pickImageCollectionLayout.minimumInteritemSpacing = 2; 163 | pickImageCollectionLayout.minimumLineSpacing = 2; 164 | _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 44) collectionViewLayout:pickImageCollectionLayout]; 165 | _collectionView.backgroundColor = [UIColor whiteColor]; 166 | _collectionView.delegate = self; 167 | _collectionView.dataSource = self; 168 | [_collectionView registerNib:[UINib nibWithNibName:@"YPhotoCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"YPhotoCollectionCellID"]; 169 | [self.view addSubview:_collectionView]; 170 | } 171 | 172 | - (void)viewDidLoad { 173 | [super viewDidLoad]; 174 | 175 | _globalVar = [YPhotoGlobalVar shareGlobalVar]; 176 | 177 | [self loadNavgationBar]; 178 | [self loadAlAsset]; 179 | [self loadCollectionView]; 180 | [self setToolBar]; 181 | } 182 | 183 | - (void)viewDidAppear:(BOOL)animated { 184 | [super viewDidAppear:animated]; 185 | 186 | self.navigationController.interactivePopGestureRecognizer.enabled = YES; 187 | 188 | //NSLog(@"_globalVar.indexPaths=%@",_globalVar.indexPaths); 189 | if ( _globalVar.reloadIndexPaths.count > 0) { 190 | [_collectionView reloadItemsAtIndexPaths:_globalVar.reloadIndexPaths]; 191 | } 192 | } 193 | 194 | - (void)viewWillAppear:(BOOL)animated { 195 | [super viewWillAppear:animated]; 196 | 197 | if (_globalVar.selectedAlassets.count > 0) { 198 | _previewBtn.enabled = YES; 199 | _selectBtn.enabled = YES; 200 | 201 | _selectNumLabel.hidden = NO; 202 | _selectNumLabel.text = [NSString stringWithFormat:@"%d",_globalVar.currentNum]; 203 | } else { 204 | _previewBtn.enabled = NO; 205 | _selectBtn.enabled = NO; 206 | 207 | _selectNumLabel.hidden = YES; 208 | } 209 | } 210 | 211 | - (void)presentTap { 212 | if (_globalVar.currentNum <= 0) { 213 | return; 214 | } 215 | 216 | UIImage * centerImage = [UIImage imageWithCGImage:[_globalVar.selectedAlassets[0] thumbnail]] ; 217 | YPhotoPreviewViewController * previewVC = [YPhotoPreviewViewController new]; 218 | previewVC.centerImage = centerImage; 219 | NSIndexPath * indepath = [NSIndexPath indexPathForRow:0 inSection:1]; 220 | previewVC.indexPath = indepath; 221 | previewVC.aLAssets = _globalVar.selectedAlassets; 222 | previewVC.lib = _lib; 223 | previewVC.isPresent = YES; 224 | [self.navigationController pushViewController:previewVC animated:YES]; 225 | } 226 | 227 | - (void)doneTap { 228 | 229 | UIActivityIndicatorView * activityIV = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]; 230 | activityIV.center = CGPointMake([UIScreen mainScreen].bounds.size.width/2, [UIScreen mainScreen].bounds.size.height/3); 231 | [self.view addSubview:activityIV]; 232 | [activityIV startAnimating]; 233 | 234 | UIImage * fullScreenImg; 235 | 236 | for (int i = 0; i < _globalVar.selectedAlassets.count; i++) { 237 | _representation = [_globalVar.selectedAlassets[i] defaultRepresentation]; 238 | 239 | fullScreenImg = [UIImage imageWithCGImage:[_representation fullScreenImage]] ; 240 | fullScreenImg = [self compressionImage:fullScreenImg WithScale:_globalVar.targetImageScale]; 241 | 242 | [_globalVar.selectedImgs addObject:fullScreenImg]; 243 | 244 | UIImage * thumbnail = [UIImage imageWithCGImage:((ALAsset *)_globalVar.selectedAlassets[i]).thumbnail]; 245 | [_globalVar.selectedThumbnails addObject:thumbnail]; 246 | } 247 | 248 | _globalVar.indexPaths = nil; 249 | _globalVar.reloadIndexPaths = nil; 250 | _globalVar.selectedAlassets = nil; 251 | _globalVar.animation = nil; 252 | 253 | [activityIV stopAnimating]; 254 | [activityIV removeFromSuperview]; 255 | 256 | [self dismissViewControllerAnimated:YES completion:nil]; 257 | 258 | } 259 | 260 | - (UIImage *)compressionImage:(UIImage *)image WithScale:(CGFloat)scale { 261 | /* 按给定尺寸 按比例缩放的方法 262 | 实现等比例缩放 263 | double hfactor = image.size.width / targetSize.width; 264 | double vfactor = image.size.height / targetSize.height; 265 | double factor = fmax(hfactor, vfactor); 266 | 绘制画布 267 | double newWith = image.size.width / factor; 268 | double newHeight = image.size.height / factor; 269 | CGSize newSize = CGSizeMake(newWith, newHeight); 270 | */ 271 | /* 272 | 按照给定缩放比例缩放 273 | */ 274 | double newWith = image.size.width * scale; 275 | double newHeight = image.size.height * scale; 276 | CGSize newSize = CGSizeMake(newWith, newHeight); 277 | 278 | UIGraphicsBeginImageContext(newSize); 279 | [image drawInRect:CGRectMake(0, 0, newWith, newHeight)]; 280 | UIImage * newImage = UIGraphicsGetImageFromCurrentImageContext(); 281 | UIGraphicsEndImageContext(); 282 | 283 | //按照jpg格式进行了压缩,如果需要png格式就把这个换成 : UIImagePNGRepresentation(newImage); 284 | NSData * data; 285 | if (_globalVar.png == YES) { 286 | data = UIImagePNGRepresentation(newImage); 287 | } 288 | else { 289 | data = UIImageJPEGRepresentation(newImage, 0.5);//二次压缩 290 | } 291 | newImage = [UIImage imageWithData:data]; 292 | 293 | return newImage; 294 | } 295 | 296 | - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { 297 | return 1; 298 | } 299 | 300 | - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { 301 | return _aLAssets.count; 302 | } 303 | - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { 304 | 305 | static NSString * cellID = @"YPhotoCollectionCellID"; 306 | __weak YPhotoCollectionViewCell * cell = (YPhotoCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath]; 307 | if ([_globalVar.indexPaths containsObject:indexPath]) { 308 | cell.isPicked = YES; 309 | [cell.button setImage:_globalVar.yesImg forState:UIControlStateNormal]; 310 | } else { 311 | cell.isPicked = NO; 312 | [cell.button setImage:_globalVar.noImg forState:UIControlStateNormal]; 313 | } 314 | 315 | cell.selectBlock = ^(BOOL isSelect) { 316 | if (isSelect == YES) { 317 | 318 | _globalVar.currentNum ++; 319 | if (_globalVar.currentNum > 0) { 320 | _previewBtn.enabled = YES; 321 | _selectBtn.enabled = YES; 322 | 323 | _selectNumLabel.hidden = NO; 324 | } 325 | _selectNumLabel.text = [NSString stringWithFormat:@"%d",_globalVar.currentNum]; 326 | [self selectCompressionImageWithIndexPath:(NSIndexPath *)indexPath]; 327 | } 328 | else if (isSelect == NO) { 329 | _globalVar.currentNum --; 330 | if (_globalVar.currentNum == 0) { 331 | _previewBtn.enabled = NO; 332 | _selectBtn.enabled = NO; 333 | 334 | _selectNumLabel.hidden = YES; 335 | } 336 | if (_globalVar.currentNum >= 0) { 337 | _selectNumLabel.text = [NSString stringWithFormat:@"%d",_globalVar.currentNum]; 338 | [self deleteCompressionImageIndexPath:(NSIndexPath *)indexPath]; 339 | } 340 | } 341 | }; 342 | cell.imageView.image = [UIImage imageWithCGImage:((ALAsset *)_aLAssets[indexPath.row]).thumbnail]; 343 | 344 | return cell; 345 | } 346 | 347 | - (void)selectCompressionImageWithIndexPath:(NSIndexPath *)indexPath { 348 | 349 | [_globalVar.selectedAlassets addObject:_aLAssets[indexPath.row]]; 350 | [_globalVar.indexPaths addObject:indexPath]; 351 | } 352 | 353 | - (void)deleteCompressionImageIndexPath:(NSIndexPath *)indexPath { 354 | [_globalVar.selectedAlassets removeObject:_aLAssets[indexPath.row]]; 355 | [_globalVar.indexPaths removeObject:indexPath]; 356 | } 357 | 358 | - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { 359 | 360 | UIImage * centerImage = [UIImage imageWithCGImage:((ALAsset *)_aLAssets[indexPath.row]).thumbnail]; 361 | YPhotoPreviewViewController * previewVC = [YPhotoPreviewViewController new]; 362 | previewVC.centerImage = centerImage; 363 | previewVC.indexPath = indexPath; 364 | previewVC.aLAssets = _aLAssets; 365 | previewVC.lib = _lib; 366 | [self.navigationController pushViewController:previewVC animated:YES]; 367 | } 368 | 369 | - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section { 370 | return UIEdgeInsetsMake(2, 2, 2, 2); 371 | } 372 | 373 | - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { 374 | CGFloat cellWH = ([UIScreen mainScreen].bounds.size.width - 10) / 4; 375 | CGSize cellSize = CGSizeMake(cellWH, cellWH); 376 | return cellSize; 377 | } 378 | 379 | //MARK: - alertViewdelegate 380 | - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { 381 | if (buttonIndex == 1) { 382 | NSURL * url = [NSURL URLWithString:UIApplicationOpenSettingsURLString]; 383 | [[UIApplication sharedApplication] openURL:url]; 384 | } 385 | } 386 | 387 | - (void)didReceiveMemoryWarning { 388 | [super didReceiveMemoryWarning]; 389 | // Dispose of any resources that can be recreated. 390 | } 391 | 392 | -(void)dealloc { 393 | //NSLog(@"thumbnail dealloc"); 394 | } 395 | 396 | @end 397 | -------------------------------------------------------------------------------- /YPhotoSelectDemo/选择照片.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 180083A11D950F7F002087E5 /* TestCollectionViewCell.m in Sources */ = {isa = PBXBuildFile; fileRef = 180083A01D950F7F002087E5 /* TestCollectionViewCell.m */; }; 11 | 18AC7EC91D9A5CE800455906 /* YPhotoAblumTableViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 18AC7EBC1D9A5CE800455906 /* YPhotoAblumTableViewController.m */; }; 12 | 18AC7ECA1D9A5CE800455906 /* YPhotoCollectionViewCell.m in Sources */ = {isa = PBXBuildFile; fileRef = 18AC7EBE1D9A5CE800455906 /* YPhotoCollectionViewCell.m */; }; 13 | 18AC7ECB1D9A5CE800455906 /* YPhotoCollectionViewCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = 18AC7EBF1D9A5CE800455906 /* YPhotoCollectionViewCell.xib */; }; 14 | 18AC7ECC1D9A5CE800455906 /* YPhotoGlobalVar.m in Sources */ = {isa = PBXBuildFile; fileRef = 18AC7EC11D9A5CE800455906 /* YPhotoGlobalVar.m */; }; 15 | 18AC7ECD1D9A5CE800455906 /* YPhotoNavViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 18AC7EC31D9A5CE800455906 /* YPhotoNavViewController.m */; }; 16 | 18AC7ECE1D9A5CE800455906 /* YPhotoPreviewViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 18AC7EC51D9A5CE800455906 /* YPhotoPreviewViewController.m */; }; 17 | 18AC7ECF1D9A5CE800455906 /* YPhotoThumbnailsViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 18AC7EC81D9A5CE800455906 /* YPhotoThumbnailsViewController.m */; }; 18 | 18AC7ED11D9A5D1500455906 /* YPhoto.bundle in Resources */ = {isa = PBXBuildFile; fileRef = 18AC7ED01D9A5D1500455906 /* YPhoto.bundle */; }; 19 | 18C023541D406A2000B858D7 /* YPhoto.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 18C023531D406A2000B858D7 /* YPhoto.storyboard */; }; 20 | 18CD2F781D3F1DEF004BC816 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 18CD2F771D3F1DEF004BC816 /* main.m */; }; 21 | 18CD2F7B1D3F1DEF004BC816 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 18CD2F7A1D3F1DEF004BC816 /* AppDelegate.m */; }; 22 | 18CD2F831D3F1DEF004BC816 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 18CD2F821D3F1DEF004BC816 /* Assets.xcassets */; }; 23 | 18CD2F861D3F1DEF004BC816 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 18CD2F841D3F1DEF004BC816 /* LaunchScreen.storyboard */; }; 24 | 18CD2F921D3F4F2C004BC816 /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 18CD2F911D3F4F2C004BC816 /* ViewController.m */; }; 25 | /* End PBXBuildFile section */ 26 | 27 | /* Begin PBXFileReference section */ 28 | 1800839F1D950F7F002087E5 /* TestCollectionViewCell.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TestCollectionViewCell.h; sourceTree = ""; }; 29 | 180083A01D950F7F002087E5 /* TestCollectionViewCell.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TestCollectionViewCell.m; sourceTree = ""; }; 30 | 18AC7EBB1D9A5CE800455906 /* YPhotoAblumTableViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = YPhotoAblumTableViewController.h; sourceTree = ""; }; 31 | 18AC7EBC1D9A5CE800455906 /* YPhotoAblumTableViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = YPhotoAblumTableViewController.m; sourceTree = ""; }; 32 | 18AC7EBD1D9A5CE800455906 /* YPhotoCollectionViewCell.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = YPhotoCollectionViewCell.h; sourceTree = ""; }; 33 | 18AC7EBE1D9A5CE800455906 /* YPhotoCollectionViewCell.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = YPhotoCollectionViewCell.m; sourceTree = ""; }; 34 | 18AC7EBF1D9A5CE800455906 /* YPhotoCollectionViewCell.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = YPhotoCollectionViewCell.xib; sourceTree = ""; }; 35 | 18AC7EC01D9A5CE800455906 /* YPhotoGlobalVar.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = YPhotoGlobalVar.h; sourceTree = ""; }; 36 | 18AC7EC11D9A5CE800455906 /* YPhotoGlobalVar.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = YPhotoGlobalVar.m; sourceTree = ""; }; 37 | 18AC7EC21D9A5CE800455906 /* YPhotoNavViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = YPhotoNavViewController.h; sourceTree = ""; }; 38 | 18AC7EC31D9A5CE800455906 /* YPhotoNavViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = YPhotoNavViewController.m; sourceTree = ""; }; 39 | 18AC7EC41D9A5CE800455906 /* YPhotoPreviewViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = YPhotoPreviewViewController.h; sourceTree = ""; }; 40 | 18AC7EC51D9A5CE800455906 /* YPhotoPreviewViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = YPhotoPreviewViewController.m; sourceTree = ""; }; 41 | 18AC7EC71D9A5CE800455906 /* YPhotoThumbnailsViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = YPhotoThumbnailsViewController.h; sourceTree = ""; }; 42 | 18AC7EC81D9A5CE800455906 /* YPhotoThumbnailsViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = YPhotoThumbnailsViewController.m; sourceTree = ""; }; 43 | 18AC7ED01D9A5D1500455906 /* YPhoto.bundle */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.plug-in"; path = YPhoto.bundle; sourceTree = ""; }; 44 | 18C023531D406A2000B858D7 /* YPhoto.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; name = YPhoto.storyboard; path = ../YPhoto.storyboard; sourceTree = ""; }; 45 | 18CD2F731D3F1DEF004BC816 /* 选择照片.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "选择照片.app"; sourceTree = BUILT_PRODUCTS_DIR; }; 46 | 18CD2F771D3F1DEF004BC816 /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; }; 47 | 18CD2F791D3F1DEF004BC816 /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = ""; }; 48 | 18CD2F7A1D3F1DEF004BC816 /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = ""; }; 49 | 18CD2F821D3F1DEF004BC816 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 50 | 18CD2F851D3F1DEF004BC816 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 51 | 18CD2F871D3F1DEF004BC816 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 52 | 18CD2F901D3F4F2C004BC816 /* ViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ViewController.h; sourceTree = ""; }; 53 | 18CD2F911D3F4F2C004BC816 /* ViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ViewController.m; sourceTree = ""; }; 54 | /* End PBXFileReference section */ 55 | 56 | /* Begin PBXFrameworksBuildPhase section */ 57 | 18CD2F701D3F1DEF004BC816 /* Frameworks */ = { 58 | isa = PBXFrameworksBuildPhase; 59 | buildActionMask = 2147483647; 60 | files = ( 61 | ); 62 | runOnlyForDeploymentPostprocessing = 0; 63 | }; 64 | /* End PBXFrameworksBuildPhase section */ 65 | 66 | /* Begin PBXGroup section */ 67 | 18AC7EBA1D9A5CE800455906 /* YPhotoSelect */ = { 68 | isa = PBXGroup; 69 | children = ( 70 | 18AC7ED01D9A5D1500455906 /* YPhoto.bundle */, 71 | 18AC7EBB1D9A5CE800455906 /* YPhotoAblumTableViewController.h */, 72 | 18AC7EBC1D9A5CE800455906 /* YPhotoAblumTableViewController.m */, 73 | 18AC7EBD1D9A5CE800455906 /* YPhotoCollectionViewCell.h */, 74 | 18AC7EBE1D9A5CE800455906 /* YPhotoCollectionViewCell.m */, 75 | 18AC7EBF1D9A5CE800455906 /* YPhotoCollectionViewCell.xib */, 76 | 18AC7EC01D9A5CE800455906 /* YPhotoGlobalVar.h */, 77 | 18AC7EC11D9A5CE800455906 /* YPhotoGlobalVar.m */, 78 | 18AC7EC21D9A5CE800455906 /* YPhotoNavViewController.h */, 79 | 18AC7EC31D9A5CE800455906 /* YPhotoNavViewController.m */, 80 | 18AC7EC41D9A5CE800455906 /* YPhotoPreviewViewController.h */, 81 | 18AC7EC51D9A5CE800455906 /* YPhotoPreviewViewController.m */, 82 | 18AC7EC71D9A5CE800455906 /* YPhotoThumbnailsViewController.h */, 83 | 18AC7EC81D9A5CE800455906 /* YPhotoThumbnailsViewController.m */, 84 | ); 85 | path = YPhotoSelect; 86 | sourceTree = ""; 87 | }; 88 | 18CD2F6A1D3F1DEF004BC816 = { 89 | isa = PBXGroup; 90 | children = ( 91 | 18CD2F751D3F1DEF004BC816 /* 选择照片 */, 92 | 18CD2F741D3F1DEF004BC816 /* Products */, 93 | ); 94 | sourceTree = ""; 95 | }; 96 | 18CD2F741D3F1DEF004BC816 /* Products */ = { 97 | isa = PBXGroup; 98 | children = ( 99 | 18CD2F731D3F1DEF004BC816 /* 选择照片.app */, 100 | ); 101 | name = Products; 102 | sourceTree = ""; 103 | }; 104 | 18CD2F751D3F1DEF004BC816 /* 选择照片 */ = { 105 | isa = PBXGroup; 106 | children = ( 107 | 18AC7EBA1D9A5CE800455906 /* YPhotoSelect */, 108 | 18CD2F791D3F1DEF004BC816 /* AppDelegate.h */, 109 | 18CD2F7A1D3F1DEF004BC816 /* AppDelegate.m */, 110 | 18C023531D406A2000B858D7 /* YPhoto.storyboard */, 111 | 1800839F1D950F7F002087E5 /* TestCollectionViewCell.h */, 112 | 180083A01D950F7F002087E5 /* TestCollectionViewCell.m */, 113 | 18CD2F901D3F4F2C004BC816 /* ViewController.h */, 114 | 18CD2F911D3F4F2C004BC816 /* ViewController.m */, 115 | 18CD2F821D3F1DEF004BC816 /* Assets.xcassets */, 116 | 18CD2F841D3F1DEF004BC816 /* LaunchScreen.storyboard */, 117 | 18CD2F871D3F1DEF004BC816 /* Info.plist */, 118 | 18CD2F761D3F1DEF004BC816 /* Supporting Files */, 119 | ); 120 | path = "选择照片"; 121 | sourceTree = ""; 122 | }; 123 | 18CD2F761D3F1DEF004BC816 /* Supporting Files */ = { 124 | isa = PBXGroup; 125 | children = ( 126 | 18CD2F771D3F1DEF004BC816 /* main.m */, 127 | ); 128 | name = "Supporting Files"; 129 | sourceTree = ""; 130 | }; 131 | /* End PBXGroup section */ 132 | 133 | /* Begin PBXNativeTarget section */ 134 | 18CD2F721D3F1DEF004BC816 /* 选择照片 */ = { 135 | isa = PBXNativeTarget; 136 | buildConfigurationList = 18CD2F8A1D3F1DEF004BC816 /* Build configuration list for PBXNativeTarget "选择照片" */; 137 | buildPhases = ( 138 | 18CD2F6F1D3F1DEF004BC816 /* Sources */, 139 | 18CD2F701D3F1DEF004BC816 /* Frameworks */, 140 | 18CD2F711D3F1DEF004BC816 /* Resources */, 141 | ); 142 | buildRules = ( 143 | ); 144 | dependencies = ( 145 | ); 146 | name = "选择照片"; 147 | productName = "选择照片"; 148 | productReference = 18CD2F731D3F1DEF004BC816 /* 选择照片.app */; 149 | productType = "com.apple.product-type.application"; 150 | }; 151 | /* End PBXNativeTarget section */ 152 | 153 | /* Begin PBXProject section */ 154 | 18CD2F6B1D3F1DEF004BC816 /* Project object */ = { 155 | isa = PBXProject; 156 | attributes = { 157 | LastUpgradeCheck = 0730; 158 | ORGANIZATIONNAME = Huyp; 159 | TargetAttributes = { 160 | 18CD2F721D3F1DEF004BC816 = { 161 | CreatedOnToolsVersion = 7.3.1; 162 | DevelopmentTeam = 66WL9H8YZP; 163 | }; 164 | }; 165 | }; 166 | buildConfigurationList = 18CD2F6E1D3F1DEF004BC816 /* Build configuration list for PBXProject "选择照片" */; 167 | compatibilityVersion = "Xcode 3.2"; 168 | developmentRegion = English; 169 | hasScannedForEncodings = 0; 170 | knownRegions = ( 171 | en, 172 | Base, 173 | ); 174 | mainGroup = 18CD2F6A1D3F1DEF004BC816; 175 | productRefGroup = 18CD2F741D3F1DEF004BC816 /* Products */; 176 | projectDirPath = ""; 177 | projectRoot = ""; 178 | targets = ( 179 | 18CD2F721D3F1DEF004BC816 /* 选择照片 */, 180 | ); 181 | }; 182 | /* End PBXProject section */ 183 | 184 | /* Begin PBXResourcesBuildPhase section */ 185 | 18CD2F711D3F1DEF004BC816 /* Resources */ = { 186 | isa = PBXResourcesBuildPhase; 187 | buildActionMask = 2147483647; 188 | files = ( 189 | 18CD2F861D3F1DEF004BC816 /* LaunchScreen.storyboard in Resources */, 190 | 18AC7ECB1D9A5CE800455906 /* YPhotoCollectionViewCell.xib in Resources */, 191 | 18AC7ED11D9A5D1500455906 /* YPhoto.bundle in Resources */, 192 | 18CD2F831D3F1DEF004BC816 /* Assets.xcassets in Resources */, 193 | 18C023541D406A2000B858D7 /* YPhoto.storyboard in Resources */, 194 | ); 195 | runOnlyForDeploymentPostprocessing = 0; 196 | }; 197 | /* End PBXResourcesBuildPhase section */ 198 | 199 | /* Begin PBXSourcesBuildPhase section */ 200 | 18CD2F6F1D3F1DEF004BC816 /* Sources */ = { 201 | isa = PBXSourcesBuildPhase; 202 | buildActionMask = 2147483647; 203 | files = ( 204 | 18CD2F7B1D3F1DEF004BC816 /* AppDelegate.m in Sources */, 205 | 18CD2F781D3F1DEF004BC816 /* main.m in Sources */, 206 | 18AC7EC91D9A5CE800455906 /* YPhotoAblumTableViewController.m in Sources */, 207 | 18AC7ECD1D9A5CE800455906 /* YPhotoNavViewController.m in Sources */, 208 | 180083A11D950F7F002087E5 /* TestCollectionViewCell.m in Sources */, 209 | 18AC7ECE1D9A5CE800455906 /* YPhotoPreviewViewController.m in Sources */, 210 | 18AC7ECC1D9A5CE800455906 /* YPhotoGlobalVar.m in Sources */, 211 | 18CD2F921D3F4F2C004BC816 /* ViewController.m in Sources */, 212 | 18AC7ECF1D9A5CE800455906 /* YPhotoThumbnailsViewController.m in Sources */, 213 | 18AC7ECA1D9A5CE800455906 /* YPhotoCollectionViewCell.m in Sources */, 214 | ); 215 | runOnlyForDeploymentPostprocessing = 0; 216 | }; 217 | /* End PBXSourcesBuildPhase section */ 218 | 219 | /* Begin PBXVariantGroup section */ 220 | 18CD2F841D3F1DEF004BC816 /* LaunchScreen.storyboard */ = { 221 | isa = PBXVariantGroup; 222 | children = ( 223 | 18CD2F851D3F1DEF004BC816 /* Base */, 224 | ); 225 | name = LaunchScreen.storyboard; 226 | sourceTree = ""; 227 | }; 228 | /* End PBXVariantGroup section */ 229 | 230 | /* Begin XCBuildConfiguration section */ 231 | 18CD2F881D3F1DEF004BC816 /* Debug */ = { 232 | isa = XCBuildConfiguration; 233 | buildSettings = { 234 | ALWAYS_SEARCH_USER_PATHS = NO; 235 | CLANG_ANALYZER_NONNULL = YES; 236 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 237 | CLANG_CXX_LIBRARY = "libc++"; 238 | CLANG_ENABLE_MODULES = YES; 239 | CLANG_ENABLE_OBJC_ARC = YES; 240 | CLANG_WARN_BOOL_CONVERSION = YES; 241 | CLANG_WARN_CONSTANT_CONVERSION = YES; 242 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 243 | CLANG_WARN_EMPTY_BODY = YES; 244 | CLANG_WARN_ENUM_CONVERSION = YES; 245 | CLANG_WARN_INT_CONVERSION = YES; 246 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 247 | CLANG_WARN_UNREACHABLE_CODE = YES; 248 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 249 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 250 | COPY_PHASE_STRIP = NO; 251 | DEBUG_INFORMATION_FORMAT = dwarf; 252 | ENABLE_STRICT_OBJC_MSGSEND = YES; 253 | ENABLE_TESTABILITY = YES; 254 | GCC_C_LANGUAGE_STANDARD = gnu99; 255 | GCC_DYNAMIC_NO_PIC = NO; 256 | GCC_NO_COMMON_BLOCKS = YES; 257 | GCC_OPTIMIZATION_LEVEL = 0; 258 | GCC_PREPROCESSOR_DEFINITIONS = ( 259 | "DEBUG=1", 260 | "$(inherited)", 261 | ); 262 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 263 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 264 | GCC_WARN_UNDECLARED_SELECTOR = YES; 265 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 266 | GCC_WARN_UNUSED_FUNCTION = YES; 267 | GCC_WARN_UNUSED_VARIABLE = YES; 268 | IPHONEOS_DEPLOYMENT_TARGET = 9.3; 269 | MTL_ENABLE_DEBUG_INFO = YES; 270 | ONLY_ACTIVE_ARCH = YES; 271 | SDKROOT = iphoneos; 272 | }; 273 | name = Debug; 274 | }; 275 | 18CD2F891D3F1DEF004BC816 /* Release */ = { 276 | isa = XCBuildConfiguration; 277 | buildSettings = { 278 | ALWAYS_SEARCH_USER_PATHS = NO; 279 | CLANG_ANALYZER_NONNULL = YES; 280 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 281 | CLANG_CXX_LIBRARY = "libc++"; 282 | CLANG_ENABLE_MODULES = YES; 283 | CLANG_ENABLE_OBJC_ARC = YES; 284 | CLANG_WARN_BOOL_CONVERSION = YES; 285 | CLANG_WARN_CONSTANT_CONVERSION = YES; 286 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 287 | CLANG_WARN_EMPTY_BODY = YES; 288 | CLANG_WARN_ENUM_CONVERSION = YES; 289 | CLANG_WARN_INT_CONVERSION = YES; 290 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 291 | CLANG_WARN_UNREACHABLE_CODE = YES; 292 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 293 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 294 | COPY_PHASE_STRIP = NO; 295 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 296 | ENABLE_NS_ASSERTIONS = NO; 297 | ENABLE_STRICT_OBJC_MSGSEND = YES; 298 | GCC_C_LANGUAGE_STANDARD = gnu99; 299 | GCC_NO_COMMON_BLOCKS = YES; 300 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 301 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 302 | GCC_WARN_UNDECLARED_SELECTOR = YES; 303 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 304 | GCC_WARN_UNUSED_FUNCTION = YES; 305 | GCC_WARN_UNUSED_VARIABLE = YES; 306 | IPHONEOS_DEPLOYMENT_TARGET = 9.3; 307 | MTL_ENABLE_DEBUG_INFO = NO; 308 | SDKROOT = iphoneos; 309 | VALIDATE_PRODUCT = YES; 310 | }; 311 | name = Release; 312 | }; 313 | 18CD2F8B1D3F1DEF004BC816 /* Debug */ = { 314 | isa = XCBuildConfiguration; 315 | buildSettings = { 316 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 317 | CODE_SIGN_IDENTITY = "iPhone Developer"; 318 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 319 | INFOPLIST_FILE = "选择照片/Info.plist"; 320 | IPHONEOS_DEPLOYMENT_TARGET = 7.0; 321 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 322 | PRODUCT_BUNDLE_IDENTIFIER = "com.TJ.----"; 323 | PRODUCT_NAME = "$(TARGET_NAME)"; 324 | PROVISIONING_PROFILE = ""; 325 | }; 326 | name = Debug; 327 | }; 328 | 18CD2F8C1D3F1DEF004BC816 /* Release */ = { 329 | isa = XCBuildConfiguration; 330 | buildSettings = { 331 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 332 | CODE_SIGN_IDENTITY = "iPhone Developer"; 333 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 334 | INFOPLIST_FILE = "选择照片/Info.plist"; 335 | IPHONEOS_DEPLOYMENT_TARGET = 7.0; 336 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 337 | PRODUCT_BUNDLE_IDENTIFIER = "com.TJ.----"; 338 | PRODUCT_NAME = "$(TARGET_NAME)"; 339 | PROVISIONING_PROFILE = ""; 340 | }; 341 | name = Release; 342 | }; 343 | /* End XCBuildConfiguration section */ 344 | 345 | /* Begin XCConfigurationList section */ 346 | 18CD2F6E1D3F1DEF004BC816 /* Build configuration list for PBXProject "选择照片" */ = { 347 | isa = XCConfigurationList; 348 | buildConfigurations = ( 349 | 18CD2F881D3F1DEF004BC816 /* Debug */, 350 | 18CD2F891D3F1DEF004BC816 /* Release */, 351 | ); 352 | defaultConfigurationIsVisible = 0; 353 | defaultConfigurationName = Release; 354 | }; 355 | 18CD2F8A1D3F1DEF004BC816 /* Build configuration list for PBXNativeTarget "选择照片" */ = { 356 | isa = XCConfigurationList; 357 | buildConfigurations = ( 358 | 18CD2F8B1D3F1DEF004BC816 /* Debug */, 359 | 18CD2F8C1D3F1DEF004BC816 /* Release */, 360 | ); 361 | defaultConfigurationIsVisible = 0; 362 | defaultConfigurationName = Release; 363 | }; 364 | /* End XCConfigurationList section */ 365 | }; 366 | rootObject = 18CD2F6B1D3F1DEF004BC816 /* Project object */; 367 | } 368 | -------------------------------------------------------------------------------- /YPhotoSelect/YPhotoPreviewViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // YPhotoPreviewViewController.m 3 | // 选择照片 4 | // 5 | // Created by 彦鹏 on 16/7/25. 6 | // Copyright © 2016年 Huyp. All rights reserved. 7 | // 8 | 9 | #import "YPhotoPreviewViewController.h" 10 | #import "YPhotoGlobalVar.h" 11 | 12 | @interface YPhotoPreviewViewController () 13 | 14 | @property (strong , nonatomic) UIScrollView * imageScrollViewA; 15 | @property (strong , nonatomic) UIScrollView * imageScrollViewB; 16 | @property (strong , nonatomic) UIScrollView * imageScrollViewC; 17 | 18 | @property (strong , nonatomic) __block UIImage * leftImage; //初始左边 19 | @property (strong , nonatomic) __block UIImage * rightImage; //初始右边 20 | 21 | @property (strong,nonatomic) UIScrollView * scrollView; 22 | @property (strong,nonatomic) UIToolbar * toolBar; 23 | @property (strong,nonatomic) UIBarButtonItem * doneItem; 24 | 25 | @property (assign,nonatomic) int pastIndexPathRow; 26 | @property (strong,nonatomic) ALAssetRepresentation * representation; 27 | @property (strong,nonatomic) YPhotoGlobalVar * globalVar; 28 | 29 | @property (strong,nonatomic) CAKeyframeAnimation * animation; 30 | @end 31 | 32 | @implementation YPhotoPreviewViewController 33 | 34 | - (void)cancel { 35 | _globalVar.selectedThumbnails = nil; 36 | _globalVar.selectedImgs = nil; 37 | _globalVar.currentNum = 0; 38 | _globalVar.animation = nil; 39 | 40 | _globalVar.indexPaths = nil; 41 | _globalVar.reloadIndexPaths = nil; 42 | _globalVar.selectedAlassets = nil; 43 | 44 | [self.navigationController popViewControllerAnimated:YES]; 45 | } 46 | 47 | - (void)done { 48 | 49 | UIActivityIndicatorView * activityIV = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]; 50 | activityIV.center = CGPointMake([UIScreen mainScreen].bounds.size.width/2, [UIScreen mainScreen].bounds.size.height/3); 51 | [self.view addSubview:activityIV]; 52 | [activityIV startAnimating]; 53 | 54 | UIImage * fullScreenImg; 55 | for (int i = 0; i < _globalVar.selectedAlassets.count; i++) { 56 | _representation = [_globalVar.selectedAlassets[i] defaultRepresentation]; 57 | 58 | fullScreenImg = [UIImage imageWithCGImage:[_representation fullScreenImage]] ; 59 | fullScreenImg = [self compressionImage:fullScreenImg WithScale:_globalVar.targetImageScale]; 60 | [_globalVar.selectedImgs addObject:fullScreenImg]; 61 | 62 | UIImage * thumbnail = [UIImage imageWithCGImage:((ALAsset *)_globalVar.selectedAlassets[i]).thumbnail]; 63 | [_globalVar.selectedThumbnails addObject:thumbnail]; 64 | } 65 | 66 | [activityIV stopAnimating]; 67 | [activityIV removeFromSuperview]; 68 | 69 | _globalVar.indexPaths = nil; 70 | _globalVar.reloadIndexPaths = nil; 71 | _globalVar.selectedAlassets = nil; 72 | _globalVar.animation = nil; 73 | 74 | [self dismissViewControllerAnimated:YES completion:nil]; 75 | 76 | } 77 | 78 | - (UIImage *)compressionImage:(UIImage *)image WithScale:(CGFloat)scale { 79 | /* 按给定尺寸 按比例缩放的方法 80 | 实现等比例缩放 81 | double hfactor = image.size.width / targetSize.width; 82 | double vfactor = image.size.height / targetSize.height; 83 | double factor = fmax(hfactor, vfactor); 84 | 绘制画布 85 | double newWith = image.size.width / factor; 86 | double newHeight = image.size.height / factor; 87 | CGSize newSize = CGSizeMake(newWith, newHeight); 88 | */ 89 | /* 90 | 按照给定缩放比例缩放 91 | */ 92 | double newWith = image.size.width * scale; 93 | double newHeight = image.size.height * scale; 94 | CGSize newSize = CGSizeMake(newWith, newHeight); 95 | 96 | UIGraphicsBeginImageContext(newSize); 97 | [image drawInRect:CGRectMake(0, 0, newWith, newHeight)]; 98 | UIImage * newImage = UIGraphicsGetImageFromCurrentImageContext(); 99 | UIGraphicsEndImageContext(); 100 | 101 | //按照jpg格式进行了压缩,如果需要png格式就把这个换成 : UIImagePNGRepresentation(newImage); 102 | NSData * data; 103 | if (_globalVar.png == YES) { 104 | data = UIImagePNGRepresentation(newImage); 105 | } 106 | else { 107 | data = UIImageJPEGRepresentation(newImage, 0.5);//二次压缩 108 | } 109 | 110 | newImage = [UIImage imageWithData:data]; 111 | 112 | return newImage; 113 | } 114 | 115 | - (void)selectOrDisSelect { 116 | 117 | 118 | if (_isPresent == YES) { 119 | //因为这部分是后补的,和其他的方法有些不同----懒得改一致了,写的时候发现了这个问题,我也挺郁闷的,原因是一开始对于逻辑没想周全. 120 | _globalVar.reloadIndexPaths = _presentIndexParhs; 121 | 122 | if (self.navigationItem.rightBarButtonItem.image == _globalVar.noImg) { 123 | if (_globalVar.maxNum == _globalVar.currentNum) { 124 | NSString * title = [NSString stringWithFormat:@"您最多只能选择%d张图片",_globalVar.maxNum]; 125 | UIAlertView * alert = [[UIAlertView alloc]initWithTitle:title message:nil delegate:nil cancelButtonTitle:@"我知道了" otherButtonTitles:nil, nil]; 126 | [alert show]; 127 | return; 128 | } 129 | 130 | if (![_globalVar.reloadIndexPaths containsObject:_presentIndexParhs[_indexPath.row]]) { 131 | [_globalVar.reloadIndexPaths addObject:_presentIndexParhs[_indexPath.row]]; 132 | } 133 | 134 | _globalVar.currentNum++; 135 | [_globalVar.indexPaths addObject:_presentIndexParhs[_indexPath.row]]; 136 | [_globalVar.selectedAlassets addObject:_aLAssets[_indexPath.row]]; 137 | self.navigationItem.rightBarButtonItem.image = _globalVar.yesImg; 138 | } 139 | else { 140 | _globalVar.currentNum--; 141 | [_globalVar.indexPaths removeObject:_presentIndexParhs[_indexPath.row]]; 142 | [_globalVar.selectedAlassets removeObject:_aLAssets[_indexPath.row]]; 143 | self.navigationItem.rightBarButtonItem.image = _globalVar.noImg; 144 | } 145 | } 146 | else { 147 | 148 | if (![_globalVar.reloadIndexPaths containsObject:_indexPath]) { 149 | [_globalVar.reloadIndexPaths addObject:_indexPath]; 150 | } 151 | 152 | if ([_globalVar.indexPaths containsObject:_indexPath]) { 153 | _globalVar.currentNum--; 154 | self.navigationItem.rightBarButtonItem.image = _globalVar.noImg; 155 | [_globalVar.indexPaths removeObject:_indexPath]; 156 | [_globalVar.selectedAlassets removeObject:_aLAssets[_indexPath.row]]; 157 | } 158 | else { 159 | if (_globalVar.maxNum == _globalVar.currentNum) { 160 | NSString * title = [NSString stringWithFormat:@"你最多只能选择%d张图片",_globalVar.maxNum]; 161 | UIAlertView * alert = [[UIAlertView alloc]initWithTitle:title message:nil delegate:nil cancelButtonTitle:@"我知道了" otherButtonTitles:nil, nil]; 162 | [alert show]; 163 | return; 164 | } 165 | 166 | _globalVar.currentNum++; 167 | self.navigationItem.rightBarButtonItem.image = _globalVar.yesImg; 168 | [_globalVar.indexPaths addObject:_indexPath]; 169 | [_globalVar.selectedAlassets addObject:_aLAssets[_indexPath.row]]; 170 | } 171 | } 172 | 173 | if (_globalVar.currentNum == 0) { 174 | _doneItem.enabled = NO; 175 | }else { 176 | _doneItem.enabled = YES; 177 | } 178 | 179 | } 180 | 181 | - (void)setNavgation { 182 | UIBarButtonItem * backItem = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(cancel)]; 183 | [backItem setTitleTextAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:15]} forState:UIControlStateNormal]; 184 | //self.navigationItem.leftBarButtonItem = backItem; 185 | 186 | self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage new] style:UIBarButtonItemStylePlain target:self action:@selector(selectOrDisSelect)]; 187 | } 188 | 189 | - (UIImage*)createImageWithColor:(UIColor*) color 190 | { 191 | CGRect rect=CGRectMake(0.0f, 0.0f, 1.0f, 1.0f); 192 | UIGraphicsBeginImageContext(rect.size); 193 | CGContextRef context = UIGraphicsGetCurrentContext(); 194 | CGContextSetFillColorWithColor(context, [color CGColor]); 195 | CGContextFillRect(context, rect); 196 | UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext(); 197 | UIGraphicsEndImageContext(); 198 | return theImage; 199 | } 200 | 201 | - (void)setToolBar { 202 | _toolBar = [UIToolbar new]; 203 | 204 | _toolBar.translatesAutoresizingMaskIntoConstraints = NO; 205 | UIImage * bgImage = [self createImageWithColor:[UIColor colorWithRed:0.1 green:0.1 blue:0.1 alpha:0.7]]; 206 | [_toolBar setBackgroundImage:bgImage forToolbarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault]; 207 | [self.view addSubview:_toolBar]; 208 | 209 | NSArray * h = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[toolBar]-0-|" options:NSLayoutFormatDirectionLeadingToTrailing metrics:nil views:@{@"toolBar":_toolBar}]; 210 | NSArray * v = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[toolBar(44)]-0-|" options:NSLayoutFormatDirectionLeadingToTrailing metrics:nil views:@{@"toolBar":_toolBar}]; 211 | 212 | UIBarButtonItem * flexibleBarBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil]; 213 | _doneItem = [[UIBarButtonItem alloc]initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(done)]; 214 | [_doneItem setTintColor:[UIColor greenColor]]; 215 | _toolBar.items = @[flexibleBarBtn,_doneItem]; 216 | if (_globalVar.currentNum == 0) { 217 | _doneItem.enabled = NO; 218 | } 219 | 220 | [self.view addConstraints:h]; 221 | [self.view addConstraints:v]; 222 | } 223 | 224 | - (void)setScrollView { 225 | ALAuthorizationStatus status = [ALAssetsLibrary authorizationStatus]; 226 | // 如果没有获取访问授权,或者访问授权状态已经被明确禁止,则显示提示语,引导用户开启授权 227 | if (status == ALAuthorizationStatusRestricted || status == ALAuthorizationStatusDenied) { 228 | UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"应用想访问您的相册" message:@"请设置应用的相册访问权限" delegate:self cancelButtonTitle:@"不允许" otherButtonTitles:@"去设置", nil]; 229 | [alert show]; 230 | } 231 | else { 232 | _scrollView = [UIScrollView new]; 233 | _scrollView.translatesAutoresizingMaskIntoConstraints = NO; 234 | _scrollView.delegate = self; 235 | [self.view addSubview:_scrollView]; 236 | NSArray * h = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(-3)-[scrollView]-(-3)-|" options:NSLayoutFormatDirectionLeftToRight metrics:nil views:@{@"scrollView":_scrollView}]; 237 | NSArray * v = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[scrollView]|" options:NSLayoutFormatDirectionLeadingToTrailing metrics:nil views:@{@"scrollView":_scrollView}]; 238 | [self.view addConstraints:h]; 239 | [self.view addConstraints:v]; 240 | 241 | _scrollView.backgroundColor = [UIColor blackColor]; 242 | _scrollView.pagingEnabled = YES; 243 | _scrollView.showsHorizontalScrollIndicator = NO; 244 | _scrollView.showsVerticalScrollIndicator = NO; 245 | _scrollView.alwaysBounceVertical = NO; 246 | _scrollView.alwaysBounceHorizontal = YES; 247 | _scrollView.contentMode = UIViewContentModeCenter; 248 | _scrollView.contentSize = CGSizeMake(([UIScreen mainScreen].bounds.size.width + 6) * _aLAssets.count, 0); 249 | _scrollView.contentOffset = CGPointMake(([UIScreen mainScreen].bounds.size.width + 6) * _indexPath.row, 0); 250 | 251 | _imageScrollViewA = [self showThumbnailImageScrollViewAtIndexPath:_indexPath.row - 1]; 252 | _imageScrollViewA.delegate = self; 253 | _imageScrollViewB = [self showThumbnailImageScrollViewAtIndexPath:_indexPath.row]; 254 | _imageScrollViewB.delegate = self; 255 | _imageScrollViewC = [self showThumbnailImageScrollViewAtIndexPath:_indexPath.row + 1]; 256 | _imageScrollViewC.delegate = self; 257 | 258 | [_scrollView addSubview:_imageScrollViewA]; 259 | [_scrollView addSubview:_imageScrollViewB]; 260 | [_scrollView addSubview:_imageScrollViewC]; 261 | 262 | _scrollView.userInteractionEnabled = YES; 263 | UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapImageHiddenNavAndToolBar:)]; 264 | [_scrollView addGestureRecognizer:tap]; 265 | 266 | if (_isPresent == YES) { 267 | self.navigationItem.rightBarButtonItem.image = _globalVar.yesImg; 268 | } 269 | else { 270 | if ([_globalVar.indexPaths containsObject:_indexPath]) { 271 | self.navigationItem.rightBarButtonItem.image = _globalVar.yesImg; 272 | } 273 | else { 274 | self.navigationItem.rightBarButtonItem.image = _globalVar.noImg; 275 | } 276 | } 277 | 278 | //异步获取fullscreen图片 279 | dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 280 | ALAssetRepresentation * representation = [_aLAssets[_indexPath.row] defaultRepresentation]; 281 | _centerImage = [UIImage imageWithCGImage:[representation fullScreenImage]] ; 282 | dispatch_async(dispatch_get_main_queue(), ^{ 283 | ((UIImageView *)(((UIScrollView *)(_scrollView.subviews[1])).subviews.firstObject)).image = _centerImage; 284 | }); 285 | }); 286 | } 287 | } 288 | 289 | - (void)tapImageHiddenNavAndToolBar:(id)sender { 290 | if (self.navigationController.navigationBar.isHidden) { 291 | self.navigationController.navigationBar.hidden = NO; 292 | [_toolBar setHidden:NO]; 293 | } 294 | else { 295 | self.navigationController.navigationBar.hidden = YES; 296 | [_toolBar setHidden:YES]; 297 | } 298 | } 299 | 300 | - (void)viewDidLoad { 301 | [super viewDidLoad]; 302 | 303 | _globalVar = [YPhotoGlobalVar shareGlobalVar]; 304 | _globalVar.reloadIndexPaths = [_globalVar.indexPaths mutableCopy]; 305 | 306 | if (_isPresent == YES) { 307 | _presentIndexParhs = [_globalVar.indexPaths mutableCopy]; 308 | _aLAssets = [_globalVar.selectedAlassets mutableCopy]; 309 | } 310 | 311 | self.automaticallyAdjustsScrollViewInsets = NO; 312 | 313 | [self setNavgation]; 314 | 315 | [self setScrollView]; 316 | 317 | [self setToolBar]; 318 | } 319 | 320 | - (void)viewDidAppear:(BOOL)animated { 321 | [super viewDidAppear:animated]; 322 | 323 | self.navigationController.interactivePopGestureRecognizer.enabled = NO; 324 | } 325 | 326 | - (UIScrollView *)showThumbnailImageScrollViewAtIndexPath:(NSInteger)indexPathRow{ 327 | 328 | UIImageView * imageView; 329 | 330 | if ((indexPathRow < 0)||(indexPathRow > _aLAssets.count - 1)) { 331 | imageView = [UIImageView new]; 332 | } 333 | else { 334 | imageView = [[UIImageView alloc]initWithImage:[self thumbnailImage:indexPathRow]]; 335 | } 336 | imageView.contentMode = UIViewContentModeScaleAspectFit; 337 | imageView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height); 338 | imageView.center = CGPointMake([UIScreen mainScreen].bounds.size.width/2, [UIScreen mainScreen].bounds.size.height/2); 339 | 340 | UIScrollView * scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(3 + ([UIScreen mainScreen].bounds.size.width + 6) * indexPathRow, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)]; 341 | scrollView.backgroundColor = [UIColor blackColor]; 342 | scrollView.showsHorizontalScrollIndicator = NO; 343 | scrollView.showsVerticalScrollIndicator = NO; 344 | scrollView.contentMode = UIViewContentModeScaleAspectFill; 345 | scrollView.minimumZoomScale = 1.0; 346 | scrollView.maximumZoomScale = 2.0; 347 | 348 | [scrollView addSubview:imageView]; 349 | 350 | return scrollView; 351 | } 352 | 353 | - (UIImage *)thumbnailImage:(NSInteger)indexPathRow { 354 | if ((indexPathRow < 0)||(indexPathRow > _aLAssets.count-1)) { 355 | return [UIImage new]; 356 | } 357 | return [UIImage imageWithCGImage:[_aLAssets[indexPathRow] aspectRatioThumbnail]]; 358 | } 359 | 360 | - (UIImage *)fullscreenImage :(NSInteger)indexPathRow { 361 | if ((indexPathRow < 0)||(indexPathRow > _aLAssets.count)) { 362 | return [UIImage new]; 363 | } 364 | ALAssetRepresentation * representation = [_aLAssets[indexPathRow] defaultRepresentation]; 365 | return [UIImage imageWithCGImage:[representation fullScreenImage]]; 366 | } 367 | 368 | - (void)didReceiveMemoryWarning { 369 | [super didReceiveMemoryWarning]; 370 | // Dispose of any resources that can be recreated. 371 | } 372 | 373 | - (void)scrollViewDidScroll:(UIScrollView *)scrollView { 374 | if (scrollView != _scrollView) { 375 | return; 376 | } 377 | 378 | //使用这个方法控制图片顺序 379 | @synchronized (self) { 380 | 381 | int indexPathRow = (int)roundf(scrollView.contentOffset.x/([UIScreen mainScreen].bounds.size.width+6)); 382 | 383 | if (indexPathRow == _indexPath.row) { 384 | return ; 385 | } 386 | else if (indexPathRow < _indexPath.row) { //向右划 显示左边的图片 387 | 388 | _indexPath = [NSIndexPath indexPathForRow:indexPathRow inSection:0]; 389 | //_indexPath.row = indexPathRow; 390 | 391 | ((UIImageView *)(((UIScrollView *)(scrollView.subviews.lastObject)).subviews.firstObject)).image = [self thumbnailImage:indexPathRow-1]; 392 | 393 | (scrollView.subviews.lastObject).frame = CGRectMake(3 + ([UIScreen mainScreen].bounds.size.width + 6) * (indexPathRow-1), 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height); 394 | 395 | [scrollView insertSubview:scrollView.subviews.lastObject atIndex:0]; 396 | 397 | ((UIImageView *)(((UIScrollView *)(scrollView.subviews.lastObject)).subviews.firstObject)).image = [self thumbnailImage:indexPathRow+1]; 398 | } 399 | else { //左滑 显示右边的图片 400 | 401 | _indexPath = [NSIndexPath indexPathForRow:indexPathRow inSection:0]; 402 | 403 | ((UIImageView *)(((UIScrollView *)(scrollView.subviews.firstObject)).subviews.firstObject)).image = [self thumbnailImage:indexPathRow+1]; 404 | 405 | (scrollView.subviews.firstObject).frame = CGRectMake(3 + ([UIScreen mainScreen].bounds.size.width + 6) * (indexPathRow+1), 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height); 406 | 407 | [scrollView insertSubview:scrollView.subviews.firstObject atIndex:2]; 408 | 409 | ((UIImageView *)(((UIScrollView *)(scrollView.subviews.firstObject)).subviews.firstObject)).image = [self thumbnailImage:indexPathRow-1]; 410 | } 411 | 412 | if (_isPresent == YES) { 413 | NSIndexPath * cuttentTrueIndexPath = _presentIndexParhs[indexPathRow]; 414 | if ([_globalVar.indexPaths containsObject:cuttentTrueIndexPath]) { 415 | self.navigationItem.rightBarButtonItem.image = _globalVar.yesImg; 416 | } 417 | else { 418 | self.navigationItem.rightBarButtonItem.image = _globalVar.noImg; 419 | } 420 | 421 | } 422 | else { 423 | if ([_globalVar.indexPaths containsObject:_indexPath]) { 424 | self.navigationItem.rightBarButtonItem.image = _globalVar.yesImg; 425 | } 426 | else { 427 | self.navigationItem.rightBarButtonItem.image = _globalVar.noImg; 428 | } 429 | } 430 | 431 | for ( UIScrollView * v in scrollView.subviews ) { 432 | [v setZoomScale:1.0]; 433 | for ( UIImageView * iv in v.subviews ) { 434 | iv.frame = [UIScreen mainScreen].bounds; 435 | } 436 | } 437 | } 438 | } 439 | 440 | - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { 441 | if (scrollView != _scrollView) { 442 | return; 443 | } 444 | //用这个方法改变图片规格 445 | //获取要显示的图片的index 过半算 446 | int indexPathRow = (int)roundf(scrollView.contentOffset.x/([UIScreen mainScreen].bounds.size.width+6)); 447 | 448 | if (indexPathRow != _aLAssets.count-1 || indexPathRow != 0) { 449 | _centerImage = [self fullscreenImage:indexPathRow]; 450 | } 451 | else if (indexPathRow == 0) { 452 | _centerImage = [self fullscreenImage:indexPathRow]; 453 | } 454 | else if (indexPathRow == _aLAssets.count-1) { 455 | _centerImage = [self fullscreenImage:indexPathRow]; 456 | } 457 | 458 | ((UIImageView *)_scrollView.subviews[1].subviews.firstObject).image = _centerImage; 459 | } 460 | 461 | //当UIScrollView尝试进行缩放的时候调用 462 | -(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView { 463 | 464 | if (scrollView != _scrollView) { 465 | return (UIImageView *)_scrollView.subviews[1].subviews.firstObject; 466 | } 467 | else { 468 | return nil; 469 | } 470 | } 471 | 472 | - (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(UIView *)view { 473 | 474 | } 475 | //当缩放完毕的时候调用 476 | - (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale { 477 | 478 | } 479 | 480 | //当正在缩放的时候调用 481 | - (void)scrollViewDidZoom:(UIScrollView *)scrollView { 482 | //NSLog(@"正在缩放 %@",scrollView); 483 | } 484 | 485 | //MARK: - alertViewdelegate 486 | - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { 487 | if (buttonIndex == 1) { 488 | NSURL * url = [NSURL URLWithString:UIApplicationOpenSettingsURLString]; 489 | [[UIApplication sharedApplication] openURL:url]; 490 | } 491 | } 492 | 493 | - (void)dealloc { 494 | //NSLog(@"present dealloc"); 495 | } 496 | @end 497 | -------------------------------------------------------------------------------- /YPhotoSelectDemo/选择照片/YPhotoSelect/YPhotoPreviewViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // YPhotoPreviewViewController.m 3 | // 选择照片 4 | // 5 | // Created by 彦鹏 on 16/7/25. 6 | // Copyright © 2016年 Huyp. All rights reserved. 7 | // 8 | 9 | #import "YPhotoPreviewViewController.h" 10 | #import "YPhotoGlobalVar.h" 11 | 12 | @interface YPhotoPreviewViewController () 13 | 14 | @property (strong , nonatomic) UIScrollView * imageScrollViewA; 15 | @property (strong , nonatomic) UIScrollView * imageScrollViewB; 16 | @property (strong , nonatomic) UIScrollView * imageScrollViewC; 17 | 18 | @property (strong , nonatomic) __block UIImage * leftImage; //初始左边 19 | @property (strong , nonatomic) __block UIImage * rightImage; //初始右边 20 | 21 | @property (strong,nonatomic) UIScrollView * scrollView; 22 | @property (strong,nonatomic) UIToolbar * toolBar; 23 | @property (strong,nonatomic) UIBarButtonItem * doneItem; 24 | 25 | @property (assign,nonatomic) int pastIndexPathRow; 26 | @property (strong,nonatomic) ALAssetRepresentation * representation; 27 | @property (strong,nonatomic) YPhotoGlobalVar * globalVar; 28 | 29 | @property (strong,nonatomic) CAKeyframeAnimation * animation; 30 | @end 31 | 32 | @implementation YPhotoPreviewViewController 33 | 34 | - (void)cancel { 35 | _globalVar.selectedThumbnails = nil; 36 | _globalVar.selectedImgs = nil; 37 | _globalVar.currentNum = 0; 38 | _globalVar.animation = nil; 39 | 40 | _globalVar.indexPaths = nil; 41 | _globalVar.reloadIndexPaths = nil; 42 | _globalVar.selectedAlassets = nil; 43 | 44 | [self.navigationController popViewControllerAnimated:YES]; 45 | } 46 | 47 | - (void)done { 48 | 49 | UIActivityIndicatorView * activityIV = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]; 50 | activityIV.center = CGPointMake([UIScreen mainScreen].bounds.size.width/2, [UIScreen mainScreen].bounds.size.height/3); 51 | [self.view addSubview:activityIV]; 52 | [activityIV startAnimating]; 53 | 54 | UIImage * fullScreenImg; 55 | for (int i = 0; i < _globalVar.selectedAlassets.count; i++) { 56 | _representation = [_globalVar.selectedAlassets[i] defaultRepresentation]; 57 | 58 | fullScreenImg = [UIImage imageWithCGImage:[_representation fullScreenImage]] ; 59 | fullScreenImg = [self compressionImage:fullScreenImg WithScale:_globalVar.targetImageScale]; 60 | [_globalVar.selectedImgs addObject:fullScreenImg]; 61 | 62 | UIImage * thumbnail = [UIImage imageWithCGImage:((ALAsset *)_globalVar.selectedAlassets[i]).thumbnail]; 63 | [_globalVar.selectedThumbnails addObject:thumbnail]; 64 | } 65 | 66 | [activityIV stopAnimating]; 67 | [activityIV removeFromSuperview]; 68 | 69 | _globalVar.indexPaths = nil; 70 | _globalVar.reloadIndexPaths = nil; 71 | _globalVar.selectedAlassets = nil; 72 | _globalVar.animation = nil; 73 | 74 | [self dismissViewControllerAnimated:YES completion:nil]; 75 | 76 | } 77 | 78 | - (UIImage *)compressionImage:(UIImage *)image WithScale:(CGFloat)scale { 79 | /* 按给定尺寸 按比例缩放的方法 80 | 实现等比例缩放 81 | double hfactor = image.size.width / targetSize.width; 82 | double vfactor = image.size.height / targetSize.height; 83 | double factor = fmax(hfactor, vfactor); 84 | 绘制画布 85 | double newWith = image.size.width / factor; 86 | double newHeight = image.size.height / factor; 87 | CGSize newSize = CGSizeMake(newWith, newHeight); 88 | */ 89 | /* 90 | 按照给定缩放比例缩放 91 | */ 92 | double newWith = image.size.width * scale; 93 | double newHeight = image.size.height * scale; 94 | CGSize newSize = CGSizeMake(newWith, newHeight); 95 | 96 | UIGraphicsBeginImageContext(newSize); 97 | [image drawInRect:CGRectMake(0, 0, newWith, newHeight)]; 98 | UIImage * newImage = UIGraphicsGetImageFromCurrentImageContext(); 99 | UIGraphicsEndImageContext(); 100 | 101 | //按照jpg格式进行了压缩,如果需要png格式就把这个换成 : UIImagePNGRepresentation(newImage); 102 | NSData * data; 103 | if (_globalVar.png == YES) { 104 | data = UIImagePNGRepresentation(newImage); 105 | } 106 | else { 107 | data = UIImageJPEGRepresentation(newImage, 0.5);//二次压缩 108 | } 109 | 110 | newImage = [UIImage imageWithData:data]; 111 | 112 | return newImage; 113 | } 114 | 115 | - (void)selectOrDisSelect { 116 | 117 | 118 | if (_isPresent == YES) { 119 | //因为这部分是后补的,和其他的方法有些不同----懒得改一致了,写的时候发现了这个问题,我也挺郁闷的,原因是一开始对于逻辑没想周全. 120 | _globalVar.reloadIndexPaths = _presentIndexParhs; 121 | 122 | if (self.navigationItem.rightBarButtonItem.image == _globalVar.noImg) { 123 | if (_globalVar.maxNum == _globalVar.currentNum) { 124 | NSString * title = [NSString stringWithFormat:@"您最多只能选择%d张图片",_globalVar.maxNum]; 125 | UIAlertView * alert = [[UIAlertView alloc]initWithTitle:title message:nil delegate:nil cancelButtonTitle:@"我知道了" otherButtonTitles:nil, nil]; 126 | [alert show]; 127 | return; 128 | } 129 | 130 | if (![_globalVar.reloadIndexPaths containsObject:_presentIndexParhs[_indexPath.row]]) { 131 | [_globalVar.reloadIndexPaths addObject:_presentIndexParhs[_indexPath.row]]; 132 | } 133 | 134 | _globalVar.currentNum++; 135 | [_globalVar.indexPaths addObject:_presentIndexParhs[_indexPath.row]]; 136 | [_globalVar.selectedAlassets addObject:_aLAssets[_indexPath.row]]; 137 | self.navigationItem.rightBarButtonItem.image = _globalVar.yesImg; 138 | } 139 | else { 140 | _globalVar.currentNum--; 141 | [_globalVar.indexPaths removeObject:_presentIndexParhs[_indexPath.row]]; 142 | [_globalVar.selectedAlassets removeObject:_aLAssets[_indexPath.row]]; 143 | self.navigationItem.rightBarButtonItem.image = _globalVar.noImg; 144 | } 145 | } 146 | else { 147 | 148 | if (![_globalVar.reloadIndexPaths containsObject:_indexPath]) { 149 | [_globalVar.reloadIndexPaths addObject:_indexPath]; 150 | } 151 | 152 | if ([_globalVar.indexPaths containsObject:_indexPath]) { 153 | _globalVar.currentNum--; 154 | self.navigationItem.rightBarButtonItem.image = _globalVar.noImg; 155 | [_globalVar.indexPaths removeObject:_indexPath]; 156 | [_globalVar.selectedAlassets removeObject:_aLAssets[_indexPath.row]]; 157 | } 158 | else { 159 | if (_globalVar.maxNum == _globalVar.currentNum) { 160 | NSString * title = [NSString stringWithFormat:@"你最多只能选择%d张图片",_globalVar.maxNum]; 161 | UIAlertView * alert = [[UIAlertView alloc]initWithTitle:title message:nil delegate:nil cancelButtonTitle:@"我知道了" otherButtonTitles:nil, nil]; 162 | [alert show]; 163 | return; 164 | } 165 | 166 | _globalVar.currentNum++; 167 | self.navigationItem.rightBarButtonItem.image = _globalVar.yesImg; 168 | [_globalVar.indexPaths addObject:_indexPath]; 169 | [_globalVar.selectedAlassets addObject:_aLAssets[_indexPath.row]]; 170 | } 171 | } 172 | 173 | if (_globalVar.currentNum == 0) { 174 | _doneItem.enabled = NO; 175 | }else { 176 | _doneItem.enabled = YES; 177 | } 178 | 179 | } 180 | 181 | - (void)setNavgation { 182 | UIBarButtonItem * backItem = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(cancel)]; 183 | [backItem setTitleTextAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:15]} forState:UIControlStateNormal]; 184 | //self.navigationItem.leftBarButtonItem = backItem; 185 | 186 | self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage new] style:UIBarButtonItemStylePlain target:self action:@selector(selectOrDisSelect)]; 187 | } 188 | 189 | - (UIImage*)createImageWithColor:(UIColor*) color 190 | { 191 | CGRect rect=CGRectMake(0.0f, 0.0f, 1.0f, 1.0f); 192 | UIGraphicsBeginImageContext(rect.size); 193 | CGContextRef context = UIGraphicsGetCurrentContext(); 194 | CGContextSetFillColorWithColor(context, [color CGColor]); 195 | CGContextFillRect(context, rect); 196 | UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext(); 197 | UIGraphicsEndImageContext(); 198 | return theImage; 199 | } 200 | 201 | - (void)setToolBar { 202 | _toolBar = [UIToolbar new]; 203 | 204 | _toolBar.translatesAutoresizingMaskIntoConstraints = NO; 205 | UIImage * bgImage = [self createImageWithColor:[UIColor colorWithRed:0.1 green:0.1 blue:0.1 alpha:0.7]]; 206 | [_toolBar setBackgroundImage:bgImage forToolbarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault]; 207 | [self.view addSubview:_toolBar]; 208 | 209 | NSArray * h = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[toolBar]-0-|" options:NSLayoutFormatDirectionLeadingToTrailing metrics:nil views:@{@"toolBar":_toolBar}]; 210 | NSArray * v = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[toolBar(44)]-0-|" options:NSLayoutFormatDirectionLeadingToTrailing metrics:nil views:@{@"toolBar":_toolBar}]; 211 | 212 | UIBarButtonItem * flexibleBarBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil]; 213 | _doneItem = [[UIBarButtonItem alloc]initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(done)]; 214 | [_doneItem setTintColor:[UIColor greenColor]]; 215 | _toolBar.items = @[flexibleBarBtn,_doneItem]; 216 | if (_globalVar.currentNum == 0) { 217 | _doneItem.enabled = NO; 218 | } 219 | 220 | [self.view addConstraints:h]; 221 | [self.view addConstraints:v]; 222 | } 223 | 224 | - (void)setScrollView { 225 | ALAuthorizationStatus status = [ALAssetsLibrary authorizationStatus]; 226 | // 如果没有获取访问授权,或者访问授权状态已经被明确禁止,则显示提示语,引导用户开启授权 227 | if (status == ALAuthorizationStatusRestricted || status == ALAuthorizationStatusDenied) { 228 | UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"应用想访问您的相册" message:@"请设置应用的相册访问权限" delegate:self cancelButtonTitle:@"不允许" otherButtonTitles:@"去设置", nil]; 229 | [alert show]; 230 | } 231 | else { 232 | _scrollView = [UIScrollView new]; 233 | _scrollView.translatesAutoresizingMaskIntoConstraints = NO; 234 | _scrollView.delegate = self; 235 | [self.view addSubview:_scrollView]; 236 | NSArray * h = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(-3)-[scrollView]-(-3)-|" options:NSLayoutFormatDirectionLeftToRight metrics:nil views:@{@"scrollView":_scrollView}]; 237 | NSArray * v = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[scrollView]|" options:NSLayoutFormatDirectionLeadingToTrailing metrics:nil views:@{@"scrollView":_scrollView}]; 238 | [self.view addConstraints:h]; 239 | [self.view addConstraints:v]; 240 | 241 | _scrollView.backgroundColor = [UIColor blackColor]; 242 | _scrollView.pagingEnabled = YES; 243 | _scrollView.showsHorizontalScrollIndicator = NO; 244 | _scrollView.showsVerticalScrollIndicator = NO; 245 | _scrollView.alwaysBounceVertical = NO; 246 | _scrollView.alwaysBounceHorizontal = YES; 247 | _scrollView.contentMode = UIViewContentModeCenter; 248 | _scrollView.contentSize = CGSizeMake(([UIScreen mainScreen].bounds.size.width + 6) * _aLAssets.count, 0); 249 | _scrollView.contentOffset = CGPointMake(([UIScreen mainScreen].bounds.size.width + 6) * _indexPath.row, 0); 250 | 251 | _imageScrollViewA = [self showThumbnailImageScrollViewAtIndexPath:_indexPath.row - 1]; 252 | _imageScrollViewA.delegate = self; 253 | _imageScrollViewB = [self showThumbnailImageScrollViewAtIndexPath:_indexPath.row]; 254 | _imageScrollViewB.delegate = self; 255 | _imageScrollViewC = [self showThumbnailImageScrollViewAtIndexPath:_indexPath.row + 1]; 256 | _imageScrollViewC.delegate = self; 257 | 258 | [_scrollView addSubview:_imageScrollViewA]; 259 | [_scrollView addSubview:_imageScrollViewB]; 260 | [_scrollView addSubview:_imageScrollViewC]; 261 | 262 | _scrollView.userInteractionEnabled = YES; 263 | UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapImageHiddenNavAndToolBar:)]; 264 | [_scrollView addGestureRecognizer:tap]; 265 | 266 | if (_isPresent == YES) { 267 | self.navigationItem.rightBarButtonItem.image = _globalVar.yesImg; 268 | } 269 | else { 270 | if ([_globalVar.indexPaths containsObject:_indexPath]) { 271 | self.navigationItem.rightBarButtonItem.image = _globalVar.yesImg; 272 | } 273 | else { 274 | self.navigationItem.rightBarButtonItem.image = _globalVar.noImg; 275 | } 276 | } 277 | 278 | //异步获取fullscreen图片 279 | dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 280 | ALAssetRepresentation * representation = [_aLAssets[_indexPath.row] defaultRepresentation]; 281 | _centerImage = [UIImage imageWithCGImage:[representation fullScreenImage]] ; 282 | dispatch_async(dispatch_get_main_queue(), ^{ 283 | ((UIImageView *)(((UIScrollView *)(_scrollView.subviews[1])).subviews.firstObject)).image = _centerImage; 284 | }); 285 | }); 286 | } 287 | } 288 | 289 | - (void)tapImageHiddenNavAndToolBar:(id)sender { 290 | if (self.navigationController.navigationBar.isHidden) { 291 | self.navigationController.navigationBar.hidden = NO; 292 | [_toolBar setHidden:NO]; 293 | } 294 | else { 295 | self.navigationController.navigationBar.hidden = YES; 296 | [_toolBar setHidden:YES]; 297 | } 298 | } 299 | 300 | - (void)viewDidLoad { 301 | [super viewDidLoad]; 302 | 303 | _globalVar = [YPhotoGlobalVar shareGlobalVar]; 304 | _globalVar.reloadIndexPaths = [_globalVar.indexPaths mutableCopy]; 305 | 306 | if (_isPresent == YES) { 307 | _presentIndexParhs = [_globalVar.indexPaths mutableCopy]; 308 | _aLAssets = [_globalVar.selectedAlassets mutableCopy]; 309 | } 310 | 311 | self.automaticallyAdjustsScrollViewInsets = NO; 312 | 313 | [self setNavgation]; 314 | 315 | [self setScrollView]; 316 | 317 | [self setToolBar]; 318 | } 319 | 320 | - (void)viewDidAppear:(BOOL)animated { 321 | [super viewDidAppear:animated]; 322 | 323 | self.navigationController.interactivePopGestureRecognizer.enabled = NO; 324 | } 325 | 326 | - (UIScrollView *)showThumbnailImageScrollViewAtIndexPath:(NSInteger)indexPathRow{ 327 | 328 | UIImageView * imageView; 329 | 330 | if ((indexPathRow < 0)||(indexPathRow > _aLAssets.count - 1)) { 331 | imageView = [UIImageView new]; 332 | } 333 | else { 334 | imageView = [[UIImageView alloc]initWithImage:[self thumbnailImage:indexPathRow]]; 335 | } 336 | imageView.contentMode = UIViewContentModeScaleAspectFit; 337 | imageView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height); 338 | imageView.center = CGPointMake([UIScreen mainScreen].bounds.size.width/2, [UIScreen mainScreen].bounds.size.height/2); 339 | 340 | UIScrollView * scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(3 + ([UIScreen mainScreen].bounds.size.width + 6) * indexPathRow, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)]; 341 | scrollView.backgroundColor = [UIColor blackColor]; 342 | scrollView.showsHorizontalScrollIndicator = NO; 343 | scrollView.showsVerticalScrollIndicator = NO; 344 | scrollView.contentMode = UIViewContentModeScaleAspectFill; 345 | scrollView.minimumZoomScale = 1.0; 346 | scrollView.maximumZoomScale = 2.0; 347 | 348 | [scrollView addSubview:imageView]; 349 | 350 | return scrollView; 351 | } 352 | 353 | - (UIImage *)thumbnailImage:(NSInteger)indexPathRow { 354 | if ((indexPathRow < 0)||(indexPathRow > _aLAssets.count-1)) { 355 | return [UIImage new]; 356 | } 357 | return [UIImage imageWithCGImage:[_aLAssets[indexPathRow] aspectRatioThumbnail]]; 358 | } 359 | 360 | - (UIImage *)fullscreenImage :(NSInteger)indexPathRow { 361 | if ((indexPathRow < 0)||(indexPathRow > _aLAssets.count)) { 362 | return [UIImage new]; 363 | } 364 | ALAssetRepresentation * representation = [_aLAssets[indexPathRow] defaultRepresentation]; 365 | return [UIImage imageWithCGImage:[representation fullScreenImage]]; 366 | } 367 | 368 | - (void)didReceiveMemoryWarning { 369 | [super didReceiveMemoryWarning]; 370 | // Dispose of any resources that can be recreated. 371 | } 372 | 373 | - (void)scrollViewDidScroll:(UIScrollView *)scrollView { 374 | if (scrollView != _scrollView) { 375 | return; 376 | } 377 | 378 | //使用这个方法控制图片顺序 379 | @synchronized (self) { 380 | 381 | int indexPathRow = (int)roundf(scrollView.contentOffset.x/([UIScreen mainScreen].bounds.size.width+6)); 382 | 383 | if (indexPathRow == _indexPath.row) { 384 | return ; 385 | } 386 | else if (indexPathRow < _indexPath.row) { //向右划 显示左边的图片 387 | 388 | _indexPath = [NSIndexPath indexPathForRow:indexPathRow inSection:0]; 389 | //_indexPath.row = indexPathRow; 390 | 391 | ((UIImageView *)(((UIScrollView *)(scrollView.subviews.lastObject)).subviews.firstObject)).image = [self thumbnailImage:indexPathRow-1]; 392 | 393 | (scrollView.subviews.lastObject).frame = CGRectMake(3 + ([UIScreen mainScreen].bounds.size.width + 6) * (indexPathRow-1), 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height); 394 | 395 | [scrollView insertSubview:scrollView.subviews.lastObject atIndex:0]; 396 | 397 | ((UIImageView *)(((UIScrollView *)(scrollView.subviews.lastObject)).subviews.firstObject)).image = [self thumbnailImage:indexPathRow+1]; 398 | } 399 | else { //左滑 显示右边的图片 400 | 401 | _indexPath = [NSIndexPath indexPathForRow:indexPathRow inSection:0]; 402 | 403 | ((UIImageView *)(((UIScrollView *)(scrollView.subviews.firstObject)).subviews.firstObject)).image = [self thumbnailImage:indexPathRow+1]; 404 | 405 | (scrollView.subviews.firstObject).frame = CGRectMake(3 + ([UIScreen mainScreen].bounds.size.width + 6) * (indexPathRow+1), 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height); 406 | 407 | [scrollView insertSubview:scrollView.subviews.firstObject atIndex:2]; 408 | 409 | ((UIImageView *)(((UIScrollView *)(scrollView.subviews.firstObject)).subviews.firstObject)).image = [self thumbnailImage:indexPathRow-1]; 410 | } 411 | 412 | if (_isPresent == YES) { 413 | NSIndexPath * cuttentTrueIndexPath = _presentIndexParhs[indexPathRow]; 414 | if ([_globalVar.indexPaths containsObject:cuttentTrueIndexPath]) { 415 | self.navigationItem.rightBarButtonItem.image = _globalVar.yesImg; 416 | } 417 | else { 418 | self.navigationItem.rightBarButtonItem.image = _globalVar.noImg; 419 | } 420 | 421 | } 422 | else { 423 | if ([_globalVar.indexPaths containsObject:_indexPath]) { 424 | self.navigationItem.rightBarButtonItem.image = _globalVar.yesImg; 425 | } 426 | else { 427 | self.navigationItem.rightBarButtonItem.image = _globalVar.noImg; 428 | } 429 | } 430 | 431 | for ( UIScrollView * v in scrollView.subviews ) { 432 | [v setZoomScale:1.0]; 433 | for ( UIImageView * iv in v.subviews ) { 434 | iv.frame = [UIScreen mainScreen].bounds; 435 | } 436 | } 437 | } 438 | } 439 | 440 | - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { 441 | if (scrollView != _scrollView) { 442 | return; 443 | } 444 | //用这个方法改变图片规格 445 | //获取要显示的图片的index 过半算 446 | int indexPathRow = (int)roundf(scrollView.contentOffset.x/([UIScreen mainScreen].bounds.size.width+6)); 447 | 448 | if (indexPathRow != _aLAssets.count-1 || indexPathRow != 0) { 449 | _centerImage = [self fullscreenImage:indexPathRow]; 450 | } 451 | else if (indexPathRow == 0) { 452 | _centerImage = [self fullscreenImage:indexPathRow]; 453 | } 454 | else if (indexPathRow == _aLAssets.count-1) { 455 | _centerImage = [self fullscreenImage:indexPathRow]; 456 | } 457 | 458 | ((UIImageView *)_scrollView.subviews[1].subviews.firstObject).image = _centerImage; 459 | } 460 | 461 | //当UIScrollView尝试进行缩放的时候调用 462 | -(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView { 463 | 464 | if (scrollView != _scrollView) { 465 | return (UIImageView *)_scrollView.subviews[1].subviews.firstObject; 466 | } 467 | else { 468 | return nil; 469 | } 470 | } 471 | 472 | - (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(UIView *)view { 473 | 474 | } 475 | //当缩放完毕的时候调用 476 | - (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale { 477 | 478 | } 479 | 480 | //当正在缩放的时候调用 481 | - (void)scrollViewDidZoom:(UIScrollView *)scrollView { 482 | //NSLog(@"正在缩放 %@",scrollView); 483 | } 484 | 485 | //MARK: - alertViewdelegate 486 | - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { 487 | if (buttonIndex == 1) { 488 | NSURL * url = [NSURL URLWithString:UIApplicationOpenSettingsURLString]; 489 | [[UIApplication sharedApplication] openURL:url]; 490 | } 491 | } 492 | 493 | - (void)dealloc { 494 | //NSLog(@"present dealloc"); 495 | } 496 | @end 497 | -------------------------------------------------------------------------------- /YPhotoSelectDemo/选择照片.xcodeproj/xcuserdata/apple.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 8 | 18 | 19 | 20 | 22 | 34 | 35 | 36 | 38 | 50 | 51 | 52 | 54 | 66 | 67 | 68 | 70 | 82 | 83 | 84 | 86 | 98 | 99 | 100 | 102 | 114 | 115 | 116 | 118 | 130 | 131 | 132 | 134 | 146 | 147 | 148 | 150 | 162 | 163 | 164 | 166 | 178 | 179 | 180 | 182 | 194 | 195 | 196 | 198 | 210 | 211 | 212 | 214 | 220 | 221 | 222 | 224 | 236 | 237 | 238 | 240 | 252 | 253 | 254 | 256 | 268 | 269 | 270 | 272 | 284 | 285 | 286 | 288 | 300 | 301 | 302 | 304 | 316 | 317 | 331 | 332 | 346 | 347 | 361 | 362 | 376 | 377 | 378 | 379 | 380 | 382 | 394 | 395 | 396 | 398 | 410 | 411 | 412 | 414 | 426 | 427 | 428 | 430 | 442 | 443 | 444 | 446 | 458 | 459 | 460 | 462 | 474 | 475 | 476 | 477 | 478 | --------------------------------------------------------------------------------