又遇到类似朋友圈的图片展示,多张9宫格,单张防止变形,Mark一下,后期可以直接用
封装了一个view,用于展示图片
YNImageView.h
#import
@class SchoolMateModel;
NS_ASSUME_NONNULL_BEGIN
typedef void(^imageViewTapBlock)(NSInteger index);
@interface YNImageView : UIView
@property (nonatomic, strong) NSArray *imgArr;
//多张图片区域的最大范围
@property (nonatomic, assign) CGFloat allWidth;
//图片点击回调
@property (nonatomic, copy) imageViewTapBlock imageViewBlock;
@end
NS_ASSUME_NONNULL_END
YNImageView.m
// Created by YN on 2019/2/27.
// Copyright © 2019年 edu268. All rights reserved.
//
#import "YNImageView.h"
#import "SchoolMateModel.h"
@interface YNImageView()
@property (nonatomic, strong) NSMutableArray *imgMutArr;
@end
@implementation YNImageView
-(NSMutableArray *)imgMutArr
{
if(!_imgMutArr){
_imgMutArr = [NSMutableArray array];
}
return _imgMutArr;
}
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
for (int i=0; i<9; i++) {
UIImageView *img = [[UIImageView alloc] init];
img.contentMode = UIViewContentModeScaleAspectFill;
img.clipsToBounds = YES;
[self addSubview:img];
[self.imgMutArr addObject:img];
}
}
return self;
}
-(void)setAllWidth:(CGFloat)allWidth
{
_allWidth = allWidth;
}
-(void)setImgArr:(NSArray *)imgArr
{
for (UIView *subView in self.imgMutArr) {
if ([subView isKindOfClass:[UIImageView class]]) {
subView.frame = CGRectZero;
}
}
CGFloat width;
//防止图片变形
if(imgArr.count == 1){
UIImageView *img = self.imgMutArr[0];
SchoolMateModel *model = imgArr[0];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick:)];
img.tag = 2120;
img.userInteractionEnabled = YES;
[img addGestureRecognizer:tap];
//先定义一个最大的框框180*180
[img sd_setImageWithURL:[model.thumbImage getURL] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
CGSize size=image.size;
CGFloat imgW,imgH;
if(size.width / size.height>1){//宽图
imgW = 180;
imgH = size.height / size.width *imgW;
}else{//窄图
imgH = 180;
imgW = size.width / size.height *imgH;
}
img.frame = CGRectMake(0, 0, imgW, imgH);
// NSLog(@"size.width:%f,size.height:%f",size.width,size.height);
}];
}else{//IMAGES_LINE_SPACE 多张图片间隙
width = (_allWidth -IMAGES_LINE_SPACE*2)/3;
for (int i=0; i
UIImageView *img = self.imgMutArr[i];
SchoolMateModel *model = imgArr[i];
img.frame = CGRectMake((width+IMAGES_LINE_SPACE)*(i%3), (width+IMAGES_LINE_SPACE)*(i/3), width, width);
[img sd_setImageWithURL:[model.thumbImage getURL] placeholderImage:EMPTY_IMG];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick:)];
img.tag = 2120+i;
img.userInteractionEnabled = YES;
[img addGestureRecognizer:tap];
}
}
}
//图片点击事件
-(void)tapClick:(UITapGestureRecognizer *)tap
{
_imageViewBlock((NSInteger)tap.view.tag-2120);
}
@end
计算图片的高度
//图片d高度 IMAGES_LINE_SPACE 多张图片间隙
+ (CGFloat)heightForContentWithWithModel:(HomeModel *)model WithWidth:(CGFloat)allWidth
{
NSMutableArray *imgArr = [NSMutableArray arrayWithArray:[SchoolMateModel mj_objectArrayWithKeyValuesArray:model.images]];
if(!allWidth){
allWidth = KSCREEN_WIDTH-73;
}
CGFloat __block customViewH = 0;
if(imgArr.count == 1){
UIImageView *img = [[UIImageView alloc] init];
SchoolMateModel *model = imgArr[0];
//单张的高度
[img sd_setImageWithURL:[model.thumbImage getURL] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
CGSize size=image.size;
CGFloat imgW;
if(size.width / size.height>1){//宽图
imgW = 180;
customViewH = size.height / size.width *imgW;
}else{//窄图
customViewH = 180;
// imgW = size.width / size.height *imgH;
}
//NSLog(@"size.width:%f,size.height:%f",size.width,size.height);
}];
}else{
if(imgArr.count >1){
CGFloat width = (allWidth -IMAGES_LINE_SPACE*2)/3;
if (imgArr.count <= 3) {
customViewH = width+IMAGES_LINE_SPACE;
}else{
if (imgArr.count % 3 == 0) {
customViewH = (imgArr.count / 3 * (width + IMAGES_LINE_SPACE));
}else
{
customViewH = ((imgArr.count / 3 + 1) * (width + IMAGES_LINE_SPACE));
}
}
}
}
return customViewH;
}
如图所示