YBIBImageLayout.m 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. //
  2. // YBIBImageLayout.m
  3. // YBImageBrowserDemo
  4. //
  5. // Created by 波儿菜 on 2019/6/12.
  6. // Copyright © 2019 波儿菜. All rights reserved.
  7. //
  8. #import "YBIBImageLayout.h"
  9. @implementation YBIBImageLayout
  10. #pragma mark - life cycle
  11. - (instancetype)init {
  12. self = [super init];
  13. if (self) {
  14. _verticalFillType = YBIBImageFillTypeCompletely;
  15. _horizontalFillType = YBIBImageFillTypeFullWidth;
  16. _zoomScaleSurplus = 1.5;
  17. }
  18. return self;
  19. }
  20. #pragma mark - private
  21. - (YBIBImageFillType)fillTypeByOrientation:(UIDeviceOrientation)orientation {
  22. return UIDeviceOrientationIsLandscape(orientation) ? self.horizontalFillType : self.verticalFillType;
  23. }
  24. #pragma mark - <YBIBImageLayout>
  25. - (CGRect)yb_imageViewFrameWithContainerSize:(CGSize)containerSize imageSize:(CGSize)imageSize orientation:(UIDeviceOrientation)orientation {
  26. if (containerSize.width <= 0 || containerSize.height <= 0 || imageSize.width <= 0 || imageSize.height <= 0) return CGRectZero;
  27. CGFloat x = 0, y = 0, width = 0, height = 0;
  28. switch ([self fillTypeByOrientation:orientation]) {
  29. case YBIBImageFillTypeFullWidth: {
  30. x = 0;
  31. width = containerSize.width;
  32. height = containerSize.width * (imageSize.height / imageSize.width);
  33. if (imageSize.width / imageSize.height >= containerSize.width / containerSize.height)
  34. y = (containerSize.height - height) / 2.0;
  35. else
  36. y = 0;
  37. }
  38. break;
  39. case YBIBImageFillTypeCompletely: {
  40. if (imageSize.width / imageSize.height >= containerSize.width / containerSize.height) {
  41. width = containerSize.width;
  42. height = containerSize.width * (imageSize.height / imageSize.width);
  43. x = 0;
  44. y = (containerSize.height - height) / 2.0;
  45. } else {
  46. height = containerSize.height;
  47. width = containerSize.height * (imageSize.width / imageSize.height);
  48. x = (containerSize.width - width) / 2.0;
  49. y = 0;
  50. }
  51. }
  52. break;
  53. default: return CGRectZero;
  54. }
  55. return CGRectMake(x, y, width, height);
  56. }
  57. - (CGFloat)yb_maximumZoomScaleWithContainerSize:(CGSize)containerSize imageSize:(CGSize)imageSize orientation:(UIDeviceOrientation)orientation {
  58. if (self.maxZoomScale >= 1) return self.maxZoomScale;
  59. if (containerSize.width <= 0 || containerSize.height <= 0) return 0;
  60. CGFloat scale = [UIScreen mainScreen].scale;
  61. if (scale <= 0) return 0;
  62. CGFloat widthScale = imageSize.width / scale / containerSize.width,
  63. heightScale = imageSize.height / scale / containerSize.height,
  64. maxScale = 1;
  65. switch ([self fillTypeByOrientation:orientation]) {
  66. case YBIBImageFillTypeFullWidth:
  67. maxScale = widthScale;
  68. break;
  69. case YBIBImageFillTypeCompletely:
  70. maxScale = MAX(widthScale, heightScale);
  71. break;
  72. default: return 0;
  73. }
  74. return MAX(maxScale, 1) * self.zoomScaleSurplus;
  75. }
  76. @end