CustomImageView.m 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. - (id)initWithImage:(UIImage *)image inset:(UIEdgeInsets)inset;
  27. {
  28. // image = [[UIImage alloc] init];
  29. if (self = [super initWithImage:image]) {
  30. self.image = [ImageUtil resizableImageWithCapInsets:inset resizingMode:UIImageResizingModeStretch img:image];
  31. }
  32. return self;
  33. }
  34. - (void)awakeFromNib {//NIB로 로드될 경우,
  35. if (CGRectEqualToRect(_rectForCap, CGRectZero)) {
  36. self.image = self.image;
  37. } else {
  38. CGFloat top, left, bottom, right;
  39. top = CGRectGetMinY(_rectForCap);
  40. left = CGRectGetMinX(_rectForCap);
  41. bottom = CGRectGetHeight(_rectForCap);
  42. right = CGRectGetWidth(_rectForCap);
  43. self.image = [ImageUtil resizableImageWithCapInsets:UIEdgeInsetsMake(top, left, bottom, right) resizingMode:UIImageResizingModeStretch img:self.image];
  44. }
  45. #ifdef DEBUG
  46. NSString *identifier = [NSString stringWithFormat:@"CustomImage:(%@)", NSStringFromCGRect(self.frame)];
  47. [self setValue:identifier forKey:@"layoutDebuggingIdentifier"];
  48. #endif
  49. }
  50. - (void)setImage:(UIImage *)image inset:(UIEdgeInsets)inset{
  51. UIImage *sImage = nil;
  52. sImage = [ImageUtil resizableImageWithCapInsets:inset resizingMode:UIImageResizingModeStretch img:image];
  53. [super setImage:sImage];
  54. }
  55. - (void)setImage:(UIImage *)image {
  56. UIImage *sImage = nil;
  57. if (!CGRectEqualToRect(_rectForCap, CGRectZero)) {
  58. sImage = image;
  59. } else {
  60. CGFloat top, left, bottom, right;
  61. top = CGRectGetMinY(_rectForCap);
  62. left = CGRectGetMinX(_rectForCap);
  63. bottom = CGRectGetHeight(_rectForCap);
  64. right = CGRectGetWidth(_rectForCap);
  65. sImage = [ImageUtil resizableImageWithCapInsets:UIEdgeInsetsMake(top, left, bottom, right) resizingMode:UIImageResizingModeStretch img:image];
  66. }
  67. [super setImage:sImage];
  68. }
  69. @end