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) (二) 动态载入图片(即只需要在程序中指定图片的路径即可载入) ...
随机推荐
- 《JAVA设计模式》之状态模式(State)
在阎宏博士的<JAVA与模式>一书中开头是这样描述状态(State)模式的: 状态模式,又称状态对象模式(Pattern of Objects for States),状态模式是对象的行为 ...
- input限制小数点的位数
在做限制input小数点的时候,我本来想通过vue里面的的watch监听来实现, ---实现逻辑是,通过监听输入的内容,当出现"."(点)的时候,记录通过indexOf获取点的位置 ...
- Python入门习题7.分别统计输入各类字符个数
例7.用户从键盘输入一行字符,编写一个程序,统计并输出其中的英文字符(包括中文字符).数字.空格和其他字符个数. #字符数统计.py Str = input('请输入一行字符:') alpha = 0 ...
- 攻防世界--csaw2013reversing2
测试文件:https://adworld.xctf.org.cn/media/task/attachments/3f35642056324371b913687e770e97e6.exe 1.准备 打开 ...
- css浮动、定位到底什么鬼?
css操作元素位置有以下几种方式:float.position.top等. I float part 1.浮动首先会先将元素在正常文档流中删除,父容器无法获取元素高度,但是该元素依然影响布局. 2.任 ...
- 2019-9-2-win10-uwp-标题栏
title author date CreateTime categories win10 uwp 标题栏 lindexi 2019-09-02 12:57:38 +0800 2018-2-13 17 ...
- linux100day(day5)--编程原理和shell脚本
通过前面的学习,我们对于linux文件系统有了一定的了解,我们接下来会初步接触编程原理和尝试编写shell脚本来实现功能. day05--编程原理和shell脚本初步认识 编程原理 在早期编程中,因为 ...
- 6层PCB设计技巧和步骤
6层PCB设计技巧和步骤 一.原理图的编辑 6层板由于PCB板中可以有两层地,所以可以将模拟地和数字地分开.对于统一地还是分开地,涉及到电磁干扰中信号的最小回流路径问题,绘制完原理图,别忘检查错误和 ...
- MySQL --13 主从复制
目录 一.主从复制简介 二.主从复制原理 三.主从复制搭建实战 四.主从复制基本故障处理 五.延时从库 企业案例: 模拟环境 一.主从复制简介 复制是 MySQL 的一项功能,允许服务器将更改从一个实 ...
- kali Linux 入门(一)
一.描述 1.基于Debian Linux 发行版 2013年3月13日 2.包含约600个安全工具 3.定制 安全稳定的内核 4.前身是BackTrack(2013年停止维护) 5.官方机构:Off ...