CustomImageView.m 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. @implementation CustomImageView
  12. #define kfRoundWidth 4.0f
  13. - (id)initWithImage:(UIImage *)image rectForCap:(CGRect)rectForCap {
  14. if (self = [super initWithImage:image]) {
  15. _rectForCap = rectForCap;
  16. CGFloat top, left, bottom, right;
  17. top = CGRectGetMinY(_rectForCap);
  18. left = CGRectGetMinX(_rectForCap);
  19. bottom = CGRectGetHeight(_rectForCap);
  20. right = CGRectGetWidth(_rectForCap);
  21. self.image = [CommonUtil resizableImageWithCapInsets:UIEdgeInsetsMake(top, left, bottom, right) resizingMode:UIImageResizingModeStretch img:self.image];
  22. }
  23. return self;
  24. }
  25. - (void)awakeFromNib {//NIB로 로드될 경우,
  26. if (CGRectEqualToRect(_rectForCap, CGRectZero)) {
  27. self.image = [CommonUtil resizableImageWithCapInsets:UIEdgeInsetsMake(kfRoundWidth, kfRoundWidth, kfRoundWidth, kfRoundWidth) resizingMode:UIImageResizingModeStretch img:self.image];
  28. } else {
  29. if (!CGRectEqualToRect(_rectForCap, CGRectMake(-1, 0, 0, 0))) {
  30. CGFloat top, left, bottom, right;
  31. top = CGRectGetMinY(_rectForCap);
  32. left = CGRectGetMinX(_rectForCap);
  33. bottom = CGRectGetHeight(_rectForCap);
  34. right = CGRectGetWidth(_rectForCap);
  35. self.image = [CommonUtil resizableImageWithCapInsets:UIEdgeInsetsMake(top, left, bottom, right) resizingMode:UIImageResizingModeStretch img:self.image];
  36. }
  37. }
  38. #ifdef DEBUG
  39. NSString *identifier = [NSString stringWithFormat:@"CustomImage:(%@)", NSStringFromCGRect(self.frame)];
  40. [self setValue:identifier forKey:@"layoutDebuggingIdentifier"];
  41. #endif
  42. }
  43. - (void)setImage:(UIImage *)image {
  44. UIImage *sImage = nil;
  45. if (CGRectEqualToRect(_rectForCap, CGRectZero)) {
  46. sImage = [CommonUtil resizableImageWithCapInsets:UIEdgeInsetsMake(kfRoundWidth, kfRoundWidth, kfRoundWidth, kfRoundWidth) resizingMode:UIImageResizingModeStretch img:image];
  47. } else {
  48. CGFloat top, left, bottom, right;
  49. top = CGRectGetMinY(_rectForCap);
  50. left = CGRectGetMinX(_rectForCap);
  51. bottom = CGRectGetHeight(_rectForCap);
  52. right = CGRectGetWidth(_rectForCap);
  53. sImage = [CommonUtil resizableImageWithCapInsets:UIEdgeInsetsMake(top, left, bottom, right) resizingMode:UIImageResizingModeStretch img:image];
  54. }
  55. [super setImage:sImage];
  56. }
  57. @end