iOS【微博项目(10)微博cell中图片的显示以及各种填充模式简介】


一效果

如果直接设置会有拉伸等等的状况,这里主要介绍图片显示的一些细节

这里写图片描述

二:代码

代码实现其实很简单,微博当中用了一个photos来存放九宫格这些图片,然后用了一个photo类来做每个photo,并且在上面显示gif等的样式,很多很多小技巧,直接上代码

九宫格根据行列设置等算法,不难

#import "HWStatusPhotosView.h"
#import "HWPhoto.h"
#import "HWStatusPhotoView.h"

#define HWStatusPhotoWH 70
#define HWStatusPhotoMargin 10
#define HWStatusPhotoMaxCol(count) ((count==4)?2:3)

@implementation HWStatusPhotosView // 9

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {

    }
    return self;
}

- (void)setPhotos:(NSArray *)photos
{
    _photos = photos;

    int photosCount = photos.count;


    // 创建足够数量的图片控件
    // 这里的self.subviews.count不要单独赋值给其他变量
    while (self.subviews.count < photosCount) {
        HWStatusPhotoView *photoView = [[HWStatusPhotoView alloc] init];
        [self addSubview:photoView];
    }

    // 遍历所有的图片控件,设置图片
    for (int i = 0; i<self.subviews.count; i++) {
        HWStatusPhotoView *photoView = self.subviews[i];

        if (i < photosCount) { // 显示
            photoView.photo = photos[i];
            photoView.hidden = NO;
        } else { // 隐藏
            photoView.hidden = YES;
        }
    }
}

- (void)layoutSubviews
{
    [super layoutSubviews];

    // 设置图片的尺寸和位置
    int photosCount = self.photos.count;
    int maxCol = HWStatusPhotoMaxCol(photosCount);
    for (int i = 0; i<photosCount; i++) {
        HWStatusPhotoView *photoView = self.subviews[i];

        int col = i % maxCol;
        photoView.x = col * (HWStatusPhotoWH + HWStatusPhotoMargin);

        int row = i / maxCol;
        photoView.y = row * (HWStatusPhotoWH + HWStatusPhotoMargin);
        photoView.width = HWStatusPhotoWH;
        photoView.height = HWStatusPhotoWH;
    }
}

+ (CGSize)sizeWithCount:(int)count
{
    // 最大列数(一行最多有多少列)
    int maxCols = HWStatusPhotoMaxCol(count);

    int cols = (count >= maxCols)? maxCols : count;
    CGFloat photosW = cols * HWStatusPhotoWH + (cols - 1) * HWStatusPhotoMargin;

    // 行数
    int rows = (count + maxCols - 1) / maxCols;
    CGFloat photosH = rows * HWStatusPhotoWH + (rows - 1) * HWStatusPhotoMargin;

    return CGSizeMake(photosW, photosH);
}
@end

photo的代码

#import "HWStatusPhotoView.h"
#import "HWPhoto.h"
#import "UIImageView+WebCache.h"

@interface HWStatusPhotoView()
@property (nonatomic, weak) UIImageView *gifView;
@end

@implementation HWStatusPhotoView

- (UIImageView *)gifView
{
    if (!_gifView) {
        UIImage *image = [UIImage imageNamed:@"timeline_image_gif"];
        UIImageView *gifView = [[UIImageView alloc] initWithImage:image];
        [self addSubview:gifView];
        self.gifView = gifView;
    }
    return _gifView;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // 内容模式
        self.contentMode = UIViewContentModeScaleAspectFill;
        // 超出边框的内容都剪掉
        self.clipsToBounds = YES;
    }
    return self;
}

- (void)setPhoto:(HWPhoto *)photo
{
    _photo = photo;

    // 设置图片
    [self sd_setImageWithURL:[NSURL URLWithString:photo.thumbnail_pic] placeholderImage:[UIImage imageNamed:@"timeline_image_placeholder"]];

    // 显示\隐藏gif控件
    // 判断是够以gif或者GIF结尾
    self.gifView.hidden = ![photo.thumbnail_pic.lowercaseString hasSuffix:@"gif"];
}

- (void)layoutSubviews
{
    [super layoutSubviews];

    self.gifView.x = self.width - self.gifView.width;
    self.gifView.y = self.height - self.gifView.height;
}

@end

三:注意地方

显示\隐藏gif控件

    // 判断是够以gif或者GIF结尾
    self.gifView.hidden = ![photo.thumbnail_pic.lowercaseString hasSuffix:@"gif"];

字符串分类根据字符串字体和最大宽度来得到所占据的高度宽度

/**
 *  根据字符串字体和最大宽度来得到所占据的高度宽度
 *
 *  @param font 字体
 *  @param maxW 最大宽度
 *
 *  @return 长宽size
 */
- (CGSize)sizeWithFont:(UIFont *)font maxW:(CGFloat)maxW
{
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    attrs[NSFontAttributeName] = font;
    CGSize maxSize = CGSizeMake(maxW, MAXFLOAT);
    return [self boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;
}
/**
 *  在宽度为最大值时候根据字体得到宽高
 *
 *  @param font 字体
 *
 *  @return 长宽size
 */
- (CGSize)sizeWithFont:(UIFont *)font
{
    return [self sizeWithFont:font maxW:MAXFLOAT];
}

UIImageView图片设置

/**
         UIViewContentModeScaleToFill : 图片拉伸至填充整个UIImageView(图片可能会变形)

         UIViewContentModeScaleAspectFit : 图片拉伸至完全显示在UIImageView里面为止(图片不会变形)

         UIViewContentModeScaleAspectFill : 
         图片拉伸至 图片的宽度等于UIImageView的宽度 或者 图片的高度等于UIImageView的高度 为止

         UIViewContentModeRedraw : 调用了setNeedsDisplay方法时,就会将图片重新渲染

         UIViewContentModeCenter : 居中显示
         UIViewContentModeTop,
         UIViewContentModeBottom,
         UIViewContentModeLeft,
         UIViewContentModeRight,
         UIViewContentModeTopLeft,
         UIViewContentModeTopRight,
         UIViewContentModeBottomLeft,
         UIViewContentModeBottomRight,

         经验规律:
         1.凡是带有Scale单词的,图片都会拉伸
         2.凡是带有Aspect单词的,图片都会保持原来的宽高比,图片不会变形
         */

        // 内容模式self(imageView对象)
        self.contentMode = UIViewContentModeScaleAspectFill;
        // 超出边框的内容都剪掉
        self.clipsToBounds = YES;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使 iOS 瀑布流的 cell 完全展示在屏幕,您需要做到以下几点: 1. 设置 UICollectionView 的 contentInset 属性,以便 cell 与屏幕边缘之间有足够的间距。 2. 在 UICollectionViewDelegateFlowLayout 协议的方法,设置每个 section 的 inset。 3. 在 UICollectionViewDelegateFlowLayout 协议的方法,确定每个 cell 的大小。 4. 在 UICollectionViewDelegate 协议的方法,使用 indexPathsForVisibleItems 属性获取当前可见的 cell。 5. 对于每个可见的 cell,使用 rectForItem(at:) 方法获取其在 UICollectionView 的 frame。 6. 使用 CGRectContainsRect() 方法检查 cell 的 frame 是否完全包含在屏幕。如果是,则 cell 完全展示在屏幕;如果不是,则需要调整 UICollectionView 的 contentOffset 属性以确保 cell 完全展示在屏幕。 以下是代码示例: ```swift func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) { let visibleCells = collectionView.indexPathsForVisibleItems for visibleIndexPath in visibleCells { if let visibleCell = collectionView.cellForItem(at: visibleIndexPath) { let cellFrame = collectionView.convert(visibleCell.frame, to: collectionView.superview) let screenFrame = UIScreen.main.bounds if !CGRectContainsRect(screenFrame, cellFrame) { let offsetY = max(0, cellFrame.maxY - screenFrame.maxY) collectionView.setContentOffset(CGPoint(x: 0, y: collectionView.contentOffset.y + offsetY), animated: true) } } } } ``` 注意,这只是一个示例代码,具体实现可能因您的需求而异。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值