EMImageBrowser.m 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. //
  2. // EMImageBrowser.m
  3. // ChatDemo-UI3.0
  4. //
  5. // Created by XieYajie on 2019/1/29.
  6. // Copyright © 2019 XieYajie. All rights reserved.
  7. //
  8. #import "EMImageBrowser.h"
  9. #import "MWPhotoBrowser.h"
  10. #define IMAGE_MAX_SIZE_5k 5120*2880
  11. static EMImageBrowser *browser = nil;
  12. @interface EMImageBrowser()<MWPhotoBrowserDelegate>
  13. @property (nonatomic, strong) NSArray *photos;
  14. @property (strong, nonatomic) MWPhotoBrowser *photoBrowser;
  15. @property (nonatomic, strong) UINavigationController *photoNavigationController;
  16. @property (nonatomic, strong) UIViewController *superController;
  17. @end
  18. @implementation EMImageBrowser
  19. + (instancetype)sharedBrowser
  20. {
  21. static dispatch_once_t onceToken;
  22. dispatch_once(&onceToken, ^{
  23. browser = [[EMImageBrowser alloc] init];
  24. });
  25. return browser;
  26. }
  27. - (instancetype)init
  28. {
  29. self = [super init];
  30. if (self) {
  31. _photoBrowser = [[MWPhotoBrowser alloc] initWithDelegate:self];
  32. _photoBrowser.delegate = self;
  33. _photoBrowser.displayActionButton = YES;
  34. _photoBrowser.displayNavArrows = YES;
  35. _photoBrowser.displaySelectionButtons = NO;
  36. _photoBrowser.alwaysShowControls = NO;
  37. _photoBrowser.zoomPhotosToFill = YES;
  38. _photoBrowser.enableGrid = NO;
  39. _photoBrowser.startOnGrid = NO;
  40. [_photoBrowser setCurrentPhotoIndex:0];
  41. _photoNavigationController = [[UINavigationController alloc] initWithRootViewController:self.photoBrowser];
  42. _photoNavigationController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
  43. }
  44. return self;
  45. }
  46. #pragma mark - MWPhotoBrowserDelegate
  47. - (NSUInteger)numberOfPhotosInPhotoBrowser:(MWPhotoBrowser *)photoBrowser
  48. {
  49. return [self.photos count];
  50. }
  51. - (id <MWPhoto>)photoBrowser:(MWPhotoBrowser *)photoBrowser photoAtIndex:(NSUInteger)index
  52. {
  53. if (index < self.photos.count) {
  54. return [self.photos objectAtIndex:index];
  55. }
  56. return nil;
  57. }
  58. - (void)photoBrowserDidFinishModalPresentation:(MWPhotoBrowser *)photoBrowser
  59. {
  60. [self dismissViewController];
  61. }
  62. #pragma mark - Private
  63. - (UIImage *)_scaleImage:(UIImage *)aImage
  64. toScale:(float)aScaleSize
  65. {
  66. UIGraphicsBeginImageContext(CGSizeMake(aImage.size.width * aScaleSize, aImage.size.height * aScaleSize));
  67. [aImage drawInRect:CGRectMake(0, 0, aImage.size.width * aScaleSize, aImage.size.height * aScaleSize)];
  68. UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
  69. UIGraphicsEndImageContext();
  70. return scaledImage;
  71. }
  72. #pragma mark - Public
  73. - (void)showImages:(NSArray<UIImage *> *)aImageArray
  74. fromController:(UIViewController *)aController
  75. {
  76. if ([aImageArray count] == 0) {
  77. return;
  78. }
  79. NSMutableArray *photoArray = [NSMutableArray array];
  80. for (id obj in aImageArray) {
  81. MWPhoto *photo;
  82. if ([obj isKindOfClass:[UIImage class]]) {
  83. UIImage *img = (UIImage *)obj;
  84. CGFloat imageSize = img.size.width * img.size.height;
  85. if (imageSize > IMAGE_MAX_SIZE_5k) {
  86. photo = [MWPhoto photoWithImage:[self _scaleImage:img toScale:(IMAGE_MAX_SIZE_5k)/imageSize]];
  87. } else {
  88. photo = [MWPhoto photoWithImage:obj];
  89. }
  90. } else if ([obj isKindOfClass:[NSURL class]]) {
  91. photo = [MWPhoto photoWithURL:obj];
  92. }
  93. if (photo) {
  94. [photoArray addObject:photo];
  95. }
  96. }
  97. self.photos = photoArray;
  98. [self.photoBrowser reloadData];
  99. self.superController = aController;
  100. [aController presentViewController:self.photoNavigationController animated:YES completion:nil];
  101. }
  102. - (void)dismissViewController
  103. {
  104. [self.superController dismissViewControllerAnimated:YES completion:^{
  105. self.superController = nil;
  106. }];
  107. }
  108. @end