工具类:自写一个简单的图片浏览器(LCGPicBrowser)
来源:互联网 发布:查看linux详细版本 编辑:程序博客网 时间:2024/05/12 18:28
#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:@"保存成功"]; }}
0 0
- 工具类:自写一个简单的图片浏览器(LCGPicBrowser)
- 一个简单的图片浏览器
- android 简单写了一个缓冲图片 json json数组,对象的工具类
- 用java写的一个简单浏览器
- Swift 写一个简单的浏览器
- WebView 写一个自己的简单浏览器
- 简单的一个图片自动轮播
- 制作一个简单的WPF图片浏览器
- 制作一个简单的WPF图片浏览器
- 一个Flex做的简单图片浏览器
- 一个简单的图片浏览器(java版)
- java建立一个简单的图片浏览器
- 使用反射写一个简单的JDBC工具类
- 自己写的一个简单mt图片幻灯类
- 写一个简单的MTK图片管理工具
- 写一个简单的MTK图片管理工具
- 利用c++写一个很简单的自相关
- 一个简单的改变图片效果的工具类(持续更新)
- 打印回型数组-回型矩阵-环形数组
- win7、win10进程pid4占用80端口的解决办法
- 通过Service播放音乐【Android】
- 欢迎使用CSDN-markdown编辑器
- 51Nod-1287-加农炮
- 工具类:自写一个简单的图片浏览器(LCGPicBrowser)
- 3D Touch-对TableView里的具体控件操作,类似微博。
- Attacking SS7: Mobile Operators Security Analysis
- redis常用场合及命令
- 迭代器设计模式和Map 遍历删除某一元素
- GridView
- 寻找父节点
- jQuery学习笔记--JqGrid相关操作 方法列表 备忘 重点讲解(超重要)
- SparryArray<E>的基本方法