CustomImageView.m 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. //
  2. // CustomImageView.m
  3. // JasonDevelop
  4. //
  5. // Created by Jason Lee on 10/25/14.
  6. // Copyright (c) jasondevelop. All rights reserved.
  7. //
  8. #import "UIView+AutoLayoutDebugging.h"
  9. #import "CommonUtil.h"
  10. #import "CustomImageView.h"
  11. #import "ImageUtil.h"
  12. @implementation CustomImageView
  13. #define kfRoundWidth 4.0f
  14. - (id)initWithImage:(UIImage *)image rectForCap:(CGRect)rectForCap {
  15. if (self = [super initWithImage:image]) {
  16. _rectForCap = rectForCap;
  17. CGFloat top, left, bottom, right;
  18. top = CGRectGetMinY(_rectForCap);
  19. left = CGRectGetMinX(_rectForCap);
  20. bottom = CGRectGetHeight(_rectForCap);
  21. right = CGRectGetWidth(_rectForCap);
  22. self.image = [ImageUtil resizableImageWithCapInsets:UIEdgeInsetsMake(top, left, bottom, right) resizingMode:UIImageResizingModeStretch img:self.image];
  23. }
  24. return self;
  25. }
  26. - (void)awakeFromNib {//NIB로 로드될 경우,
  27. if (CGRectEqualToRect(_rectForCap, CGRectZero)) {
  28. self.image = [ImageUtil resizableImageWithCapInsets:UIEdgeInsetsMake(kfRoundWidth, kfRoundWidth, kfRoundWidth, kfRoundWidth) resizingMode:UIImageResizingModeStretch img:self.image];
  29. } else {
  30. if (!CGRectEqualToRect(_rectForCap, CGRectMake(-1, 0, 0, 0))) {
  31. CGFloat top, left, bottom, right;
  32. top = CGRectGetMinY(_rectForCap);
  33. left = CGRectGetMinX(_rectForCap);
  34. bottom = CGRectGetHeight(_rectForCap);
  35. right = CGRectGetWidth(_rectForCap);
  36. self.image = [ImageUtil resizableImageWithCapInsets:UIEdgeInsetsMake(top, left, bottom, right) resizingMode:UIImageResizingModeStretch img:self.image];
  37. }
  38. }
  39. #ifdef DEBUG
  40. NSString *identifier = [NSString stringWithFormat:@"CustomImage:(%@)", NSStringFromCGRect(self.frame)];
  41. [self setValue:identifier forKey:@"layoutDebuggingIdentifier"];
  42. #endif
  43. }
  44. - (void)setImage:(UIImage *)image {
  45. UIImage *sImage = nil;
  46. if (CGRectEqualToRect(_rectForCap, CGRectZero)) {
  47. sImage = [ImageUtil resizableImageWithCapInsets:UIEdgeInsetsMake(kfRoundWidth, kfRoundWidth, kfRoundWidth, kfRoundWidth) resizingMode:UIImageResizingModeStretch img:image];
  48. } else {
  49. CGFloat top, left, bottom, right;
  50. top = CGRectGetMinY(_rectForCap);
  51. left = CGRectGetMinX(_rectForCap);
  52. bottom = CGRectGetHeight(_rectForCap);
  53. right = CGRectGetWidth(_rectForCap);
  54. sImage = [ImageUtil resizableImageWithCapInsets:UIEdgeInsetsMake(top, left, bottom, right) resizingMode:UIImageResizingModeStretch img:image];
  55. }
  56. [super setImage:sImage];
  57. }
  58. @end