新浪微博客户端(30)-制作微博中的九宫格相册图片

 

DJStatusPhotosView.h

复制代码
#import <UIKit/UIKit.h>


@interface DJStatusPhotosView : UIView


@property (nonatomic,strong) NSArray *photos;

/** 根据相册图片个数计算相册尺寸 */
+ (CGSize)sizeWithCount:(NSUInteger)count;

@end
复制代码

 

DJStatusPhotosView.m

复制代码
#import "DJStatusPhotosView.h"
#import "UIImageView+WebCache.h"
#import "DJPhoto.h"

// 相册的最大列数
#define DJStatusPhotosViewMaxColumns 3
// 相册中的图片间距
#define DJStatusPhotosViewMargin 4
// 相册图片的宽高
#define DJStatusPhotoWH ((DJContentWidth - (DJStatusPhotosViewMaxColumns - 1) * DJStatusPhotosViewMargin) / DJStatusPhotosViewMaxColumns)

@implementation DJStatusPhotosView



- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
//        self.backgroundColor = [UIColor redColor];
    }
    return self;
}



+ (CGSize)sizeWithCount:(NSUInteger)count {
    
    
    // 相册列数
    NSUInteger cols = (count >= DJStatusPhotosViewMaxColumns ) ? DJStatusPhotosViewMaxColumns : count;
    // 相册宽度
    CGFloat photosW = cols * DJStatusPhotoWH + (cols-1) * DJStatusPhotosViewMargin;
    
    
    /*
     *  补充公式:
     *  一共268条数据,每页18条,共有多少页,计算公式如下
     *  int pages = (totalCount + maxNumsInPage - 1) / maxNumsInPage
     */
    
    
    // 相册行数
    NSUInteger rows = (count + DJStatusPhotosViewMaxColumns - 1) / DJStatusPhotosViewMaxColumns;
    CGFloat photosH = rows * DJStatusPhotoWH + (rows-1) * DJStatusPhotosViewMargin;
    
    return CGSizeMake(photosW,photosH);
}



- (void)setPhotos:(NSArray *)photos {

    _photos = photos;
    
    // 添加和创建所需的UIImageView,并且设置图片的显示内容
    NSUInteger photosCount = photos.count;
    
    // 令相册里的photoView的个数始终满足待显示的需要,若数组中的图片个数大于相册中现有的个数,则创建新的photoView
    while (self.subviews.count < photosCount) {
        UIImageView *photoView = [[UIImageView alloc] init];
        [self addSubview:photoView];
    }
    
    // 设置相册中的imageView的显示内容
    for (int i = 0; i < self.subviews.count; i++) {
        UIImageView *photoView = self.subviews[i];
        if (i >= photosCount) { // 当前photoView多余,将其hidden属性置为YES,因为tableView的cell会重复利用
            photoView.hidden = YES;
        } else {
            photoView.hidden = NO;
            DJPhoto *photo = photos[i];
            [photoView sd_setImageWithURL:[NSURL URLWithString:photo.thumbnail_pic] placeholderImage:[UIImage imageNamed:@"timeline_image_placeholder"]];
        }
        
    }

}


// 在此方法中设置相册中单个图片的frame
- (void)layoutSubviews {

    [super layoutSubviews];
    
    for (int i = 0; i < self.photos.count; i++) {
        UIImageView *photoView = self.subviews[i];
        int col = i % DJStatusPhotosViewMaxColumns;
        photoView.x = col * (DJStatusPhotoWH + DJStatusPhotosViewMargin);
        
        int row = i / DJStatusPhotosViewMaxColumns;
        photoView.y = row * (DJStatusPhotoWH + DJStatusPhotosViewMargin);
        
        photoView.width = DJStatusPhotoWH;
        photoView.height = DJStatusPhotoWH;
    }
    
}




@end
复制代码

最终效果:

 



如果您觉得阅读本文对您有帮助,请点一下“推荐”按钮,您的“推荐”将是我最大的写作动力!欢迎各位转载,但是未经作者本人同意,转载文章之后必须在文章页面明显位置给出作者和原文连接,否则保留追究法律责任的权利。
posted @   夜行过客  阅读(1049)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
点击右上角即可分享
微信分享提示