SNKNinePatchImageView.m 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. //
  2. // SNKNinePatchImageView.m
  3. // TestImageResible
  4. //
  5. // Created by tu jinqiu on 2018/11/7.
  6. // Copyright © 2018年 tu jinqiu. All rights reserved.
  7. //
  8. #import "SNKNinePatchImageView.h"
  9. #import "SNKNinePatchImageCache.h"
  10. #import "UIImageView+YYWebImage.h"
  11. #import "YYDiskCache.h"
  12. @interface SNKNinePatchImageView ()
  13. @property(nonatomic, weak) NSLayoutConstraint *topConstraint;
  14. @property(nonatomic, weak) NSLayoutConstraint *leftConstraint;
  15. @property(nonatomic, weak) NSLayoutConstraint *bottomConstraint;
  16. @property(nonatomic, weak) NSLayoutConstraint *rightConstraint;
  17. @end
  18. @implementation SNKNinePatchImageView
  19. - (instancetype)initWithFrame:(CGRect)frame
  20. {
  21. if (self = [super initWithFrame:frame]) {
  22. _imageScale = 1;
  23. }
  24. return self;
  25. }
  26. - (instancetype)initWithCoder:(NSCoder *)aDecoder
  27. {
  28. if (self = [super initWithCoder:aDecoder]) {
  29. _imageScale = 1;
  30. }
  31. return self;
  32. }
  33. - (void)addConstraintsWithPaddingView:(UIView *)paddingView
  34. {
  35. NSAssert(self.superview && paddingView.superview && self.superview == paddingView.superview, @"paddingView 和 SNKNinePatchImageView 应该公有一个父 view");
  36. self.translatesAutoresizingMaskIntoConstraints = NO;
  37. [self setContentCompressionResistancePriority:UILayoutPriorityDefaultLow forAxis:UILayoutConstraintAxisHorizontal];
  38. [self setContentCompressionResistancePriority:UILayoutPriorityDefaultLow forAxis:UILayoutConstraintAxisVertical];
  39. [self setContentHuggingPriority:UILayoutPriorityDefaultLow forAxis:UILayoutConstraintAxisHorizontal];
  40. [self setContentHuggingPriority:UILayoutPriorityDefaultLow forAxis:UILayoutConstraintAxisVertical];
  41. NSLayoutConstraint *topConstraint = [NSLayoutConstraint constraintWithItem:paddingView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1.0 constant:0];
  42. self.topConstraint = topConstraint;
  43. NSLayoutConstraint *leftConstraint = [NSLayoutConstraint constraintWithItem:paddingView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0];
  44. self.leftConstraint = leftConstraint;
  45. NSLayoutConstraint *bottomConstraint = [NSLayoutConstraint constraintWithItem:paddingView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0];
  46. self.bottomConstraint = bottomConstraint;
  47. NSLayoutConstraint *rightConstraint = [NSLayoutConstraint constraintWithItem:paddingView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeRight multiplier:1.0 constant:0];
  48. self.rightConstraint = rightConstraint;
  49. [self.superview addConstraints:@[topConstraint, leftConstraint, bottomConstraint, rightConstraint]];
  50. if (self.ninePatchImage) {
  51. [self p_updateConstraints];
  52. }
  53. }
  54. - (void)setImageWithUrlStr:(NSString *)urlStr
  55. {
  56. SNKNinePatchImage *ninePatchImage = [SNKNinePatchImageCache ninePatchImageNamed:urlStr];
  57. if (ninePatchImage) {
  58. self.ninePatchImage = ninePatchImage;
  59. return;
  60. }
  61. __weak SNKNinePatchImageView *weakSelf = self;
  62. [self setImageWithURL:[NSURL URLWithString:urlStr]
  63. placeholder:nil
  64. options:kNilOptions
  65. progress:nil
  66. transform:^UIImage * _Nullable(UIImage * _Nonnull image, NSURL * _Nonnull url) {
  67. [weakSelf p_checkSetImageForUrl:url urlStr:urlStr];
  68. return image;
  69. }
  70. completion:^(UIImage * _Nullable image, NSURL * _Nonnull url, YYWebImageFromType from, YYWebImageStage stage, NSError * _Nullable error) {
  71. [weakSelf p_checkSetImageForUrl:url urlStr:urlStr];
  72. }];
  73. }
  74. - (void)setNinePatchImage:(SNKNinePatchImage *)ninePatchImage
  75. {
  76. _ninePatchImage = ninePatchImage;
  77. self.image = ninePatchImage.image;
  78. if (ninePatchImage) {
  79. [self p_updateConstraints];
  80. }
  81. }
  82. #pragma mark - private
  83. - (void)p_updateConstraints
  84. {
  85. UIEdgeInsets padding = self.ninePatchImage.paddingCap.padding;
  86. self.topConstraint.constant = padding.top;
  87. self.leftConstraint.constant = padding.left;
  88. self.bottomConstraint.constant = -padding.bottom;
  89. self.rightConstraint.constant = -padding.right;
  90. }
  91. - (void)p_checkSetImageForUrl:(NSURL *)url urlStr:(NSString *)urlStr
  92. {
  93. YYWebImageManager *manager = [YYWebImageManager sharedManager];
  94. NSString *key = [manager cacheKeyForURL:url];
  95. NSData *data = (NSData *)[[YYWebImageManager sharedManager].cache getImageDataForKey:key];
  96. if (data) {
  97. [self p_checkSetImageWithData:data urlStr:urlStr];
  98. } else {
  99. __weak SNKNinePatchImageView *weakSelf = self;
  100. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  101. NSData *data = (NSData *)[[YYWebImageManager sharedManager].cache getImageDataForKey:key];
  102. [weakSelf p_checkSetImageWithData:data urlStr:urlStr];
  103. });
  104. }
  105. }
  106. - (void)p_checkSetImageWithData:(NSData *)data urlStr:(NSString *)urlStr
  107. {
  108. SNKNinePatchImage *ninePatchImage = [SNKNinePatchImage ninePatchImageWithImageData:data scale:self.imageScale];
  109. [SNKNinePatchImageCache setNinePatchImage:ninePatchImage forName:urlStr];
  110. self.ninePatchImage = ninePatchImage;
  111. }
  112. @end