98 lines
3.6 KiB
Objective-C
98 lines
3.6 KiB
Objective-C
//
|
|
// JXCategoryImageCell.m
|
|
// JXCategoryView
|
|
//
|
|
// Created by jiaxin on 2018/8/20.
|
|
// Copyright © 2018年 jiaxin. All rights reserved.
|
|
//
|
|
|
|
#import "JXCategoryImageCell.h"
|
|
#import "JXCategoryImageCellModel.h"
|
|
|
|
@interface JXCategoryImageCell()
|
|
@property (nonatomic, strong) id currentImageInfo;
|
|
@property (nonatomic, strong) NSString *currentImageName;
|
|
@property (nonatomic, strong) NSURL *currentImageURL;
|
|
@end
|
|
|
|
@implementation JXCategoryImageCell
|
|
|
|
- (void)prepareForReuse {
|
|
[super prepareForReuse];
|
|
|
|
self.currentImageInfo = nil;
|
|
self.currentImageName = nil;
|
|
self.currentImageURL = nil;
|
|
}
|
|
|
|
- (void)initializeViews {
|
|
[super initializeViews];
|
|
|
|
_imageView = [[UIImageView alloc] init];
|
|
_imageView.contentMode = UIViewContentModeScaleAspectFit;
|
|
[self.contentView addSubview:_imageView];
|
|
}
|
|
|
|
- (void)layoutSubviews {
|
|
[super layoutSubviews];
|
|
|
|
JXCategoryImageCellModel *myCellModel = (JXCategoryImageCellModel *)self.cellModel;
|
|
self.imageView.bounds = CGRectMake(0, 0, myCellModel.imageSize.width, myCellModel.imageSize.height);
|
|
self.imageView.center = self.contentView.center;
|
|
if (myCellModel.imageCornerRadius && (myCellModel.imageCornerRadius != 0)) {
|
|
self.imageView.layer.cornerRadius = myCellModel.imageCornerRadius;
|
|
self.imageView.layer.masksToBounds = YES;
|
|
}
|
|
}
|
|
|
|
- (void)reloadData:(JXCategoryBaseCellModel *)cellModel {
|
|
[super reloadData:cellModel];
|
|
|
|
JXCategoryImageCellModel *myCellModel = (JXCategoryImageCellModel *)cellModel;
|
|
//因为`- (void)reloadData:(JXCategoryBaseCellModel *)cellModel`方法会回调多次,尤其是左右滚动的时候会调用无数次,如果每次都触发图片加载,会非常消耗性能。所以只会在图片发生了变化的时候,才进行图片加载。
|
|
if (myCellModel.loadImageBlock != nil) {
|
|
id currentImageInfo = myCellModel.imageInfo;
|
|
if (myCellModel.isSelected) {
|
|
currentImageInfo = myCellModel.selectedImageInfo;
|
|
}
|
|
if (currentImageInfo && ![currentImageInfo isEqual:self.currentImageInfo]) {
|
|
self.currentImageInfo = currentImageInfo;
|
|
if (myCellModel.loadImageBlock) {
|
|
myCellModel.loadImageBlock(self.imageView, currentImageInfo);
|
|
}
|
|
}
|
|
} else {
|
|
NSString *currentImageName;
|
|
NSURL *currentImageURL;
|
|
if (myCellModel.imageName) {
|
|
currentImageName = myCellModel.imageName;
|
|
} else if (myCellModel.imageURL) {
|
|
currentImageURL = myCellModel.imageURL;
|
|
}
|
|
if (myCellModel.isSelected) {
|
|
if (myCellModel.selectedImageName) {
|
|
currentImageName = myCellModel.selectedImageName;
|
|
} else if (myCellModel.selectedImageURL) {
|
|
currentImageURL = myCellModel.selectedImageURL;
|
|
}
|
|
}
|
|
if (currentImageName && ![currentImageName isEqualToString:self.currentImageName]) {
|
|
self.currentImageName = currentImageName;
|
|
self.imageView.image = [UIImage imageNamed:currentImageName];
|
|
} else if (currentImageURL && ![currentImageURL.absoluteString isEqualToString:self.currentImageURL.absoluteString]) {
|
|
self.currentImageURL = currentImageURL;
|
|
if (myCellModel.loadImageCallback) {
|
|
myCellModel.loadImageCallback(self.imageView, currentImageURL);
|
|
}
|
|
}
|
|
}
|
|
|
|
if (myCellModel.isImageZoomEnabled) {
|
|
self.imageView.transform = CGAffineTransformMakeScale(myCellModel.imageZoomScale, myCellModel.imageZoomScale);
|
|
}else {
|
|
self.imageView.transform = CGAffineTransformIdentity;
|
|
}
|
|
}
|
|
|
|
@end
|