JXCategoryImageCell.m 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. //
  2. // JXCategoryImageCell.m
  3. // JXCategoryView
  4. //
  5. // Created by jiaxin on 2018/8/20.
  6. // Copyright © 2018年 jiaxin. All rights reserved.
  7. //
  8. #import "JXCategoryImageCell.h"
  9. #import "JXCategoryImageCellModel.h"
  10. @interface JXCategoryImageCell()
  11. @property (nonatomic, strong) NSString *currentImageName;
  12. @property (nonatomic, strong) NSURL *currentImageURL;
  13. @end
  14. @implementation JXCategoryImageCell
  15. - (void)prepareForReuse {
  16. [super prepareForReuse];
  17. self.currentImageName = nil;
  18. self.currentImageURL = nil;
  19. }
  20. - (void)initializeViews {
  21. [super initializeViews];
  22. _imageView = [[UIImageView alloc] init];
  23. _imageView.contentMode = UIViewContentModeScaleAspectFit;
  24. _imageView.layer.masksToBounds = YES;
  25. [self.contentView addSubview:_imageView];
  26. }
  27. - (void)layoutSubviews {
  28. [super layoutSubviews];
  29. JXCategoryImageCellModel *myCellModel = (JXCategoryImageCellModel *)self.cellModel;
  30. self.imageView.bounds = CGRectMake(0, 0, myCellModel.imageSize.width, myCellModel.imageSize.height);
  31. self.imageView.center = self.contentView.center;
  32. self.imageView.layer.cornerRadius = myCellModel.imageCornerRadius;
  33. }
  34. - (void)reloadData:(JXCategoryBaseCellModel *)cellModel {
  35. [super reloadData:cellModel];
  36. JXCategoryImageCellModel *myCellModel = (JXCategoryImageCellModel *)cellModel;
  37. //因为`- (void)reloadData:(JXCategoryBaseCellModel *)cellModel`方法会回调多次,尤其是左右滚动的时候会调用无数次,如果每次都触发图片加载,会非常消耗性能。所以只会在图片发生了变化的时候,才进行图片加载。
  38. NSString *currentImageName = nil;
  39. NSURL *currentImageURL = nil;
  40. if (myCellModel.imageName != nil) {
  41. currentImageName = myCellModel.imageName;
  42. }else if (myCellModel.imageURL != nil) {
  43. currentImageURL = myCellModel.imageURL;
  44. }
  45. if (myCellModel.isSelected) {
  46. if (myCellModel.selectedImageName != nil) {
  47. currentImageName = myCellModel.selectedImageName;
  48. }else if (myCellModel.selectedImageURL != nil) {
  49. currentImageURL = myCellModel.selectedImageURL;
  50. }
  51. }
  52. if (currentImageName != nil && ![currentImageName isEqualToString:self.currentImageName]) {
  53. self.currentImageName = currentImageName;
  54. self.imageView.image = [UIImage imageNamed:currentImageName];
  55. }else if (currentImageURL != nil && ![currentImageURL.absoluteString isEqualToString:self.currentImageURL.absoluteString]) {
  56. self.currentImageURL = currentImageURL;
  57. if (myCellModel.loadImageCallback != nil) {
  58. myCellModel.loadImageCallback(self.imageView, currentImageURL);
  59. }
  60. }
  61. if (myCellModel.isImageZoomEnabled) {
  62. self.imageView.transform = CGAffineTransformMakeScale(myCellModel.imageZoomScale, myCellModel.imageZoomScale);
  63. }else {
  64. self.imageView.transform = CGAffineTransformIdentity;
  65. }
  66. }
  67. @end