Closed cheng77 closed 6 years ago
我在源码基础上,进行了优化在- (UICollectionViewCell )collectionView:(UICollectionView )collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath方法中加入缓存机制。
(UICollectionViewCell )collectionView:(UICollectionView )collectionView cellForItemAtIndexPath:(NSIndexPath )indexPath { SDCollectionViewCell cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath]; long itemIndex = indexPath.item % self.imagePathsGroup.count;
NSString *imagePath = self.imagePathsGroup[itemIndex];
if (!self.onlyDisplayText && [imagePath isKindOfClass:[NSString class]]) { if ([imagePath hasPrefix:@"http"]) {
NSURL *url = [NSURL URLWithString:imagePath]; SDWebImageManager *manager = [SDWebImageManager sharedManager]; BOOL existBool = [manager diskImageExistsForURL:url];//判断是否有缓存 UIImage * image; if (existBool) { image = [[manager imageCache] imageFromDiskCacheForKey:url.absoluteString]; }else{ NSData *data = [NSData dataWithContentsOfURL:url]; image = [UIImage imageWithData:data]; } image = [self imageCompressWithSimple:image scaledToSize:CGSizeMake([UIScreen mainScreen].bounds.size.width,[UIScreen mainScreen].bounds.size.width/2)]; cell.imageView.image = image; } else { UIImage *image = [UIImage imageNamed:imagePath]; if (!image) { [UIImage imageWithContentsOfFile:imagePath]; } cell.imageView.image = [self imageCompressWithSimple:image scaledToSize:CGSizeMake([UIScreen mainScreen].bounds.size.width,[UIScreen mainScreen].bounds.size.width/2)];; }
} else if (!self.onlyDisplayText && [imagePath isKindOfClass:[UIImage class]]) {
cell.imageView.image = [self imageCompressWithSimple:(UIImage*)imagePath scaledToSize:CGSizeMake([UIScreen mainScreen].bounds.size.width,[UIScreen mainScreen].bounds.size.width/2)];
}
if (_titlesGroup.count && itemIndex < _titlesGroup.count) { cell.title = _titlesGroup[itemIndex]; }
if (!cell.hasConfigured) { cell.titleLabelBackgroundColor = self.titleLabelBackgroundColor; cell.titleLabelHeight = self.titleLabelHeight; cell.titleLabelTextColor = self.titleLabelTextColor; cell.titleLabelTextFont = self.titleLabelTextFont; cell.hasConfigured = YES; cell.imageView.contentMode = self.bannerImageViewContentMode; cell.clipsToBounds = YES; cell.onlyDisplayText = self.onlyDisplayText; }
return cell; }
是轮播图那里吗?大图显示不完整感觉是contentmode选择不合适吧。
我在源码基础上,进行了优化在- (UICollectionViewCell )collectionView:(UICollectionView )collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath方法中加入缓存机制。
(UICollectionViewCell )collectionView:(UICollectionView )collectionView cellForItemAtIndexPath:(NSIndexPath )indexPath { SDCollectionViewCell cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath]; long itemIndex = indexPath.item % self.imagePathsGroup.count;
NSString *imagePath = self.imagePathsGroup[itemIndex];
if (!self.onlyDisplayText && [imagePath isKindOfClass:[NSString class]]) { if ([imagePath hasPrefix:@"http"]) {
} else if (!self.onlyDisplayText && [imagePath isKindOfClass:[UIImage class]]) {
}
if (_titlesGroup.count && itemIndex < _titlesGroup.count) { cell.title = _titlesGroup[itemIndex]; }
if (!cell.hasConfigured) { cell.titleLabelBackgroundColor = self.titleLabelBackgroundColor; cell.titleLabelHeight = self.titleLabelHeight; cell.titleLabelTextColor = self.titleLabelTextColor; cell.titleLabelTextFont = self.titleLabelTextFont; cell.hasConfigured = YES; cell.imageView.contentMode = self.bannerImageViewContentMode; cell.clipsToBounds = YES; cell.onlyDisplayText = self.onlyDisplayText; }
return cell; }