DKAnimationImageView.m 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. //
  2. // ZLAnimationImageView.m
  3. // ZLAssetsPickerDemo
  4. //
  5. //
  6. #import "DKAnimationImageView.h"
  7. #import "DKPhotoPickerBrowserPhoto.h"
  8. #import "UIView+Extension.h"
  9. #import "UIImageView+WebCache.h"
  10. #import "DKPhotoPickerCommon.h"
  11. #import <AssetsLibrary/AssetsLibrary.h>
  12. @interface DKAnimationImageView ()
  13. //@property (nonatomic , strong) NSMutableArray *photos;
  14. //@property (nonatomic , strong) NSDictionary *options;
  15. @end
  16. static NSArray *_photos;
  17. static UIImageView *_imageView;
  18. static NSDictionary *_options;
  19. @implementation DKAnimationImageView
  20. + (NSArray *)photos{
  21. if (!_photos) {
  22. _photos = [NSArray array];
  23. }
  24. return _photos;
  25. }
  26. + (UIImageView *)imageView{
  27. if (!_imageView) {
  28. UIImageView *imageView = [[UIImageView alloc] init];
  29. imageView.contentMode = UIViewContentModeScaleAspectFit;
  30. imageView.clipsToBounds = YES;
  31. DKAnimationBaseView *baseView = [DKAnimationBaseView sharedManager];
  32. [baseView addSubview:imageView];
  33. _imageView = imageView;
  34. }
  35. return _imageView;
  36. }
  37. + (instancetype)animationViewWithOptions:(NSDictionary *)options animations:(void (^)())animations completion:(void (^)(DKAnimationBaseView *))completion{
  38. NSAssert([options[UIViewAnimationImages] isKindOfClass:[NSArray class]], @"只能传图片数组!");
  39. _photos = options[UIViewAnimationImages];
  40. if ([(UIView*)options[UIViewAnimationToView] width] == [(UIView*)options[UIViewAnimationToView] height]) {
  41. self.imageView.contentMode = UIViewContentModeScaleAspectFill;
  42. }else{
  43. self.imageView.contentMode = UIViewContentModeScaleAspectFit;
  44. }
  45. NSMutableDictionary *ops = [NSMutableDictionary dictionaryWithDictionary:options];
  46. // 根据索引值来赋值
  47. NSIndexPath *indexPath = options[UIViewAnimationTypeViewWithIndexPath];
  48. // 设置图片
  49. [self setingPhotoImageAtIndex:indexPath.row ];
  50. ops[UIViewAnimationSelfView] = [self imageView];
  51. _options = ops;
  52. return [super animationViewWithOptions:ops animations:animations completion:completion];
  53. }
  54. + (void)restoreWithOptions:(NSDictionary *)options animation:(void (^)())completion{
  55. [self setingPhotoImageAtIndex:[[self currentIndexPath] item]];
  56. [super restoreWithOptions:options animation:completion];
  57. }
  58. #pragma make - 设置图片
  59. + (void) setingPhotoImageAtIndex:(NSInteger)index{
  60. if (!_photos.count) {
  61. return ;
  62. }
  63. if (index >= _photos.count) {
  64. index = _photos.count - 1;
  65. }
  66. DKPhotoPickerBrowserPhoto *photo = _photos[index];//[self photoWithAtIndex:self.currentPage];
  67. UIButton *cView = _options[UIViewAnimationToView];
  68. UIImage *image = nil;
  69. if (photo.photoImage) {
  70. image = photo.photoImage;
  71. }else if (photo.thumbImage){
  72. image = photo.thumbImage;
  73. }else if (photo.photoURL){
  74. [[self imageView] sd_setImageWithURL:photo.photoURL placeholderImage:nil];
  75. return;
  76. }
  77. if (!image) {
  78. if ([cView isKindOfClass:[UIButton class]]) {
  79. image = cView.imageView.image;
  80. }else if ([cView isKindOfClass:[UIImageView class]]){
  81. UIImageView *ig = (UIImageView *)cView;
  82. image = ig.image;
  83. }
  84. }
  85. [[self imageView] setImage:image];
  86. }
  87. @end