#import <UIKit/UIKit.h>
@interface LCGPicBrowser : UIView
@property (nonatomic,strong) UIScrollView *scrollView;
@property (nonatomic,strong) UILabel *countLB;
@property (nonatomic,strong) NSMutableArray *images;
@property (nonatomic,strong) UITapGestureRecognizer *currentTap;
@property (nonatomic, copy) void (^smallerBlock)(UITapGestureRecognizer *tap);
@property (nonatomic, copy) void (^saveBlock)(UILongPressGestureRecognizer *tap);
/**
* 快速创建图片浏览器
*
* @param rect 图片浏览器的大小
* @param images 图片数组
* @param currentTap 当前手势(用于取出当前点击的图片)
* @param smallerBlock 点击缩小
* @param saveBlock 长按保存
*
* @return 图片浏览器
*/
+ (instancetype)createTheLCGPicBrowser:(CGRect)rect images:(NSMutableArray *)images currentTap:(UITapGestureRecognizer *)currentTap smaller:(void(^)(UITapGestureRecognizer *tap))smallerBlock save:(void(^)(UILongPressGestureRecognizer *tap))saveBlock;
@end
#import "LCGPicBrowser.h"
@interface LCGPicBrowser ()<UIScrollViewDelegate>
@end
@implementation LCGPicBrowser
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
[self setPicBrowser];
}
return self;
}
//快速创建方法
+ (instancetype)createTheLCGPicBrowser:(CGRect)rect images:(NSMutableArray *)images currentTap:(UITapGestureRecognizer *)currentTap smaller:(void(^)(UITapGestureRecognizer *tap))smallerBlock save:(void(^)(UILongPressGestureRecognizer *tap))saveBlock {
LCGPicBrowser *picBrowser = [[LCGPicBrowser alloc]initWithFrame:rect];
picBrowser.backgroundColor = [UIColor clearColor];
picBrowser.images = images;
picBrowser.currentTap = currentTap;
picBrowser.smallerBlock = smallerBlock;
picBrowser.saveBlock = saveBlock;
CGFloat totalWidth = 0;
//往 scrollView 添加图片 or scrollView(长图)
for (int i = 0; i < picBrowser.images.count; i++) {
//计算总宽度
totalWidth = totalWidth + S_W;
//创建imageview
UIImageView *sourceImg = (UIImageView *)picBrowser.images[i];
float proportion = sourceImg.image.size.width / sourceImg.image.size.height;
UIImageView *imageView = [[UIImageView alloc]initWithImage:sourceImg.image];
imageView.backgroundColor = [UIColor whiteColor];
//设置 frame
if ((S_H - S_W / proportion) * 0.5 >= 0) {
imageView.frame = CGRectMake(S_W * i, (S_H - S_W / proportion) * 0.5, S_W, S_W / proportion);
}else {
imageView.frame = CGRectMake(S_W * i, 0, S_W, S_W / proportion);
}
//判断需不需要加 scrollView
if (imageView.height > S_H) {
//是长图则需要再加一个 scrollView
UIScrollView *subScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(S_W * i, 0, S_W, S_H)];
subScrollView.contentSize = CGSizeMake(S_W, imageView.height);
imageView.x = 0;
imageView.y = 0;
[subScrollView addSubview:imageView];
[picBrowser.scrollView addSubview: subScrollView];
}else {
//不是长图则直接添加到父 scrollView 上
[picBrowser.scrollView addSubview:imageView];
}
}
//设置 contensize
picBrowser.scrollView.contentSize = CGSizeMake(totalWidth, 0);
//移动到当前图片
[picBrowser.scrollView setContentOffset:CGPointMake(S_W * currentTap.view.tag, 0) animated:NO];
//设置页数
picBrowser.countLB = [[UILabel alloc]initWithFrame:CGRectMake(picBrowser.centerX - 30, 30, 60, 35)];
picBrowser.countLB.text = [NSString stringWithFormat:@"%ld/%ld",currentTap.view.tag + 1,picBrowser.images.count];
picBrowser.countLB.textAlignment = NSTextAlignmentCenter;
picBrowser.countLB.textColor = [UIColor whiteColor];
picBrowser.countLB.font = [UIFont systemFontOfSize:20.0f];
[picBrowser addSubview:picBrowser.countLB];
return picBrowser;
}
- (void)setPicBrowser {
//创建 scrollView
self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.width, self.height)];
self.scrollView.backgroundColor = [UIColor blackColor];
self.scrollView.alpha = 0;
self.scrollView.delegate = self;
self.scrollView.tag = 1000;
self.scrollView.showsHorizontalScrollIndicator = NO;
//分页
self.scrollView.pagingEnabled = YES;
//点击退出的手势
UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(smallerBroswer:)];
[self.scrollView addGestureRecognizer:tap1];
//长按保存手势
UILongPressGestureRecognizer *longPressGest = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressView:)];
longPressGest.minimumPressDuration = 0.5;
longPressGest.allowableMovement = 30;
[self.scrollView addGestureRecognizer:longPressGest];
[self addSubview:self.scrollView];
//渐变出场动画
[UIView animateWithDuration:0.2 animations:^{
self.scrollView.alpha = 1;
}];
}
//长按图片会调用此方法
- (void)longPressView:(UILongPressGestureRecognizer *)longPressGest {
if (self.saveBlock) {
self.saveBlock(longPressGest);
}
}
//点击缩成小图
- (void)smallerBroswer:(UITapGestureRecognizer *)tap {
if (self.smallerBlock) {
self.smallerBlock(tap);
}
}
#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
if (scrollView.tag == 1000) {
self.countLB.text = [NSString stringWithFormat:@"%.f/%ld",scrollView.contentOffset.x / S_W + 1,self.images.count];
}
}
@end
/****************具体使用*************/
//屏幕宽高
#define S_W [UIScreen mainScreen].bounds.size.width
#define S_H [UIScreen mainScreen].bounds.size.height
//头部声明
@property (nonatomic,strong) NSMutableArray *images;//储存要浏览的imageview 的数组
@property (nonatomic,strong) LCGPicBrowser *picBroswer;
#pragma mark - 点击图片放大
//点击显示大图
- (void)clickToBiggerImage:(UITapGestureRecognizer *)tap{
[self.images removeAllObjects];
DetailCommunityCell *cell = (DetailCommunityCell *)tap.view.superview.superview.superview.superview;
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
LCGAllArticleModel *tempModel = self.allArticleArr[indexPath.row];
NSMutableArray *picURLArr = (NSMutableArray *)[NSMutableArray arrayWithArray:[tempModel.pics componentsSeparatedByString:@";"]];
CGFloat totalWidth = 0;
for (int i = 0; i < picURLArr.count; i++) {
NSString *temp = picURLArr[i];
if (![temp isEqualToString:@""] && ![temp isEqualToString:@"null"] && ![temp isKindOfClass:[NSNull class]]) {
UIImageView *imageView = [[UIImageView alloc]init];
[imageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://%@:%@%@", IP, port, picURLArr[i]]] placeholderImage:[UIImage imageNamed:@"jiazaishibai"]];
[self.images addObject:imageView];
totalWidth = totalWidth + S_W;
}
}
__weak typeof(self) weakSelf = self;
self.picBroswer = [LCGPicBrowser createTheLCGPicBrowser:CGRectMake(0, 0, S_W, S_H) images:self.images currentTap:tap smaller:^(UITapGestureRecognizer *tap) {
[UIView animateWithDuration:0.2 animations:^{
self.picBroswer.alpha = 0;
self.picBroswer.countLB.alpha = 0;
}completion:^(BOOL finished) {
[self.picBroswer removeFromSuperview];
[self.picBroswer removeFromSuperview];
}];
} save:^(UILongPressGestureRecognizer *tap) {
[weakSelf longPressView:tap];
}];
[self.view addSubview:self.picBroswer];
}
//长按图片会调用此方法
- (void)longPressView:(UILongPressGestureRecognizer *)longPressGest {
UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
//取消
UIAlertAction *cancelAct = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
//保存图片
UIAlertAction *saveAct = [UIAlertAction actionWithTitle:@"保存图片" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//拿到图片
UIScrollView *scrollView = (UIScrollView *)longPressGest.view;
float num = scrollView.contentOffset.x / S_W;
UIImageView *imageView = (UIImageView *)self.images[[[NSString stringWithFormat:@"%.f",num]integerValue]];
//保存图片
UIImageWriteToSavedPhotosAlbum(imageView.image, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
}];
//显示弹框
[alertVC addAction:saveAct];
[alertVC addAction:cancelAct];
[self.navigationController presentViewController:alertVC animated:YES completion:nil];
}
//保存图片回调方法
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
if (error) {
[self showTheTipsHUDWithString:@"保存失败"];
} else {
[self showTheTipsHUDWithString:@"保存成功"];
}
}