CATiledLayer显示超大图片的解决方案
先对图片进行了裁剪 -> 很多小图片, 然后再根据显示 进行绘制
- (void)viewDidLoad {
[super viewDidLoad];
[self cutImageAndSave];
[self addTiledLayer];
}
/**
* 平铺layer 可用于展示大图
* 展示大图时可能会引起卡顿(阻塞主线程),将大图切分成小图,然后用到他们(需要展示)的时候再加载(读取)
*/
- (void)addTiledLayer{
//BingWallpaper-2015-11-22.jpg
UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:self.view.bounds];
[self.view addSubview:scrollView];
// UIImage *image = [UIImage imageNamed:@"BingWallpaper-2015-11-22.jpg"];
CATiledLayer *tiledLayer = [CATiledLayer layer];
//layer->像素 和 点 的概念不同 一个点是[UIScreen mainScreen].scale个像素
// CGFloat screenScale = [UIScreen mainScreen].scale;
// tiledLayer.contentsScale = screenScale;
tiledLayer.frame = CGRectMake(, , , );//image.size.width, image.size.height);
tiledLayer.delegate = self;
_tiledLayer = tiledLayer;
scrollView.contentSize = tiledLayer.frame.size;
//CGSizeMake(image.size.width / screenScale, image.size.height / screenScale);
[scrollView.layer addSublayer:tiledLayer];
[tiledLayer setNeedsDisplay];
}
/** 切图并保存到沙盒中 */
- (void)cutImageAndSave{
NSString *filePath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
NSString *imageName = [NSString stringWithFormat:@"%@/test-00-00.png",filePath];
UIImage *tileImage = [UIImage imageWithContentsOfFile:imageName];
NSLog(@"%@",imageName);
if (tileImage) return;
UIImage *image = [UIImage imageNamed:@"test-00-00.png"];
UIImageView *imageView = [[UIImageView alloc]initWithImage:image];
// [self.view addSubview:imageView];
CGFloat WH = ;
CGSize size = image.size;
//ceil 向上取整
NSInteger rows = ceil(size.height / WH);
NSInteger cols = ceil(size.width / WH);
for (NSInteger y = ; y < rows; ++y) {
for (NSInteger x = ; x < cols; ++x) {
UIImage *subImage = [self captureView:imageView frame:CGRectMake(x*WH, y*WH, WH, WH)];
NSString *path = [NSString stringWithFormat:@"%@/test-%02ld-%02ld.png",filePath,x,y];
[UIImagePNGRepresentation(subImage) writeToFile:path atomically:YES];
}
}
}
/** 切图 */
- (UIImage*)captureView:(UIView *)theView frame:(CGRect)fra{
//开启图形上下文 将heView的所有内容渲染到图形上下文中
UIGraphicsBeginImageContext(theView.frame.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[theView.layer renderInContext:context];
//获取图片
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGImageRef ref = CGImageCreateWithImageInRect(img.CGImage, fra);
UIImage *i = [UIImage imageWithCGImage:ref];
CGImageRelease(ref);
return i;
}
/**
* 加载图片
* CALayerDelegate
* 支持多线程绘制,-drawLayer:inContext:方法可以在多个线程中同时地并发调用
* 所以请小心谨慎地确保你在这个方法中实现的绘制代码是线程安全的.(不懂哎)
*/
- (void)drawLayer:(CATiledLayer *)layer inContext:(CGContextRef)ctx{
//获取图形上下文的位置与大小
CGRect bounds = CGContextGetClipBoundingBox(ctx);
//floor 向下取整
NSInteger x = floor(bounds.origin.x / layer.tileSize.width);
// * [UIScreen mainScreen].scale);
NSInteger y = floor(bounds.origin.y / layer.tileSize.height);
// * [UIScreen mainScreen].scale);
//load tile image
NSString *filePath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
NSString *imageName = [NSString stringWithFormat:@"%@/test-%02ld-%02ld.png",filePath,x,y];
UIImage *tileImage = [UIImage imageWithContentsOfFile:imageName];
UIGraphicsPushContext(ctx);
//绘制
[tileImage drawInRect:bounds];
UIGraphicsPopContext();
}
CATiledLayer显示超大图片的解决方案的更多相关文章
- 超大图片显示,可任意缩放,移动,不用DiskLruCache
1.演示,代码 下载示例apk 下载项目 : https://gitee.com/xi/LImage.git 2.遇到的问题 想省内存,不太可能 只支持拖拽手势,不支持缩放相对简单,解码v ...
- Atitit. IE8.0 显示本地图片预览解决方案 img.src=本地图片路径无效的解决方案
Atitit. IE8.0 显示本地图片预览解决方案 img.src=本地图片路径无效的解决方案 1. IE8.0 显示本地图片 img.src=本地图片路径无效的解决方案1 1.1. div来完成 ...
- ueditor1.4.3jsp版成功上传图片后却回显不出来与在线管理显示不出图片的解决方案
这是因为路径问题,可以在jsp/config.json这个文件去改路径 通过“imageUrlPrefix”与“imagePathFormat”这两个属性去拼凑路径. “imageUrlPrefix” ...
- 关于如何显示Jianshu图片的方案
问题的提出 简书是一个很好的博客网站,很多朋友都在jianshu上进行创作.当然出于各种目的,我们可能想将简书的文章同步到其他网站. 这个时候你会发现所有的文章里面的图片都无法正常显示了. 原因 如果 ...
- Xamarin.Android 入门之:Bind java的jar文件+Android显示gif图片
一.引言 在xamarin开发的时候,有时我们想要做一个功能,但是这个功能已经有人用java写好了,并且打包成了jar文件.那么我们可以直接把对方的jar文件拿过来用而不是重新用c#写代码. 关于bi ...
- Atitit 手机图片备份解决方案attilax总结
Atitit 手机图片备份解决方案attilax总结 1.1. 图片分类 相机图片与app图片1 1.2. 增量备份,只能使用按照时间法备份..1 1.3. 备份工具选型1 1.4. App图片,只好 ...
- Atitit. html 使用js显示本地图片的设计方案.doc
Atitit. html 使用js显示本地图片的设计方案.doc 1. Local mode 是可以的..web模式走有的不能兰.1 2. IE8.0 显示本地图片 img.src=本地图片路径无 ...
- vue项目中遇到的打印,以及处理重新排版后不显示echarts图片问题。
1. 项目中用到的打印 页面: css: 控制好宽度一般A4 我调试的是794px多了放不下,小了填不满.当时多页打印的时候,一定要控制好每一个页面内容显示的高度不要超过一个页面,当然根据自己项目来. ...
- MFC对话框显示BMP图片
1.MFC对话框显示BMP图片我们先从简单的开始吧.先分一个类: (一) 非动态显示图片(即图片先通过资源管理器载入,有一个固定ID) (二) 动态载入图片(即只需要在程序中指定图片的路径即可载入) ...
随机推荐
- ssh公私钥免密登陆
简介ssh Secure Shell(简写SSH) 为一项建立在应用层和传输层基础上的安全协议,专门为远程登录会话和其他网络服务提供安全性的协议. SSH安全机制分为两种,一种是基于口令的安全认证,一 ...
- mybatis插件机制及分页插件原理
MyBatis 插件原理与自定义插件: MyBatis 通过提供插件机制,让我们可以根据自己的需要去增强MyBatis 的功能.需要注意的是,如果没有完全理解MyBatis 的运行原理和插件的工作方式 ...
- mybatis配置mapper.xml 的基本操作
XML 映射文件 本文参考mybatis中文官网进行学习总结:http://www.mybatis.org/mybatis-3/zh/sqlmap-xml.html MyBatis 的真正强大在于它的 ...
- 【Python—参数】*arg与**kwargs参数的用法
在python中,这两个是python中的可变参数,*arg表示任意多个无名参数,类型为tuple;**kwargs表示关键字参数,为dict. # *允许你传入0个或任意个参数,这些可变参数在函数调 ...
- python安装numpy
命令介绍: D:\computerSoft\python3.6.4\Scripts>python36 pip3.6.exe install numpy # 通过pip下载对应版本的numpy,然 ...
- Centos 7.6安装mysql服务端5.7
环境:centos 7.6,mysql server 5.7.26 新建文件夹/opt/mysql,并cd进去 运行wget http://dev.mysql.com/get/mysql-5.7.26 ...
- AJAX 获取Servlet文件路径
下面均不行: xmlRes.open("get","edu/hust/ajax/TestServlet",true); xmlRes.open("ge ...
- 【Vue 2.X】基于ElementUI 实现 dialog弹窗移动效果-自定义指令系列(二)
v-dialogDrag: 弹窗拖拽 使用: <el-dialog XXX v-dialogDrag></el-dialog> Vue.directive('dialogDra ...
- JVM(2)之 JAVA堆
开发十年,就只剩下这套架构体系了! >>> 之前我们说到了栈,它在内存中是连续的空间:保存一个个的栈帧,对应一次次方法的调用:还讲到了他是保存对象的引用,那么对象存在哪里呢?我们 ...
- 基于TensorFlow Object Detection API进行迁移学习训练自己的人脸检测模型(二)
前言 已完成数据预处理工作,具体参照: 基于TensorFlow Object Detection API进行迁移学习训练自己的人脸检测模型(一) 设置配置文件 新建目录face_faster_rcn ...