先对图片进行了裁剪 -> 很多小图片, 然后再根据显示 进行绘制

- (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显示超大图片的解决方案的更多相关文章

  1. 超大图片显示,可任意缩放,移动,不用DiskLruCache

    1.演示,代码 下载示例apk      下载项目 :  https://gitee.com/xi/LImage.git 2.遇到的问题 想省内存,不太可能 只支持拖拽手势,不支持缩放相对简单,解码v ...

  2. Atitit. IE8.0 显示本地图片预览解决方案 img.src=本地图片路径无效的解决方案

    Atitit. IE8.0 显示本地图片预览解决方案 img.src=本地图片路径无效的解决方案 1. IE8.0 显示本地图片 img.src=本地图片路径无效的解决方案1 1.1. div来完成  ...

  3. ueditor1.4.3jsp版成功上传图片后却回显不出来与在线管理显示不出图片的解决方案

    这是因为路径问题,可以在jsp/config.json这个文件去改路径 通过“imageUrlPrefix”与“imagePathFormat”这两个属性去拼凑路径. “imageUrlPrefix” ...

  4. 关于如何显示Jianshu图片的方案

    问题的提出 简书是一个很好的博客网站,很多朋友都在jianshu上进行创作.当然出于各种目的,我们可能想将简书的文章同步到其他网站. 这个时候你会发现所有的文章里面的图片都无法正常显示了. 原因 如果 ...

  5. Xamarin.Android 入门之:Bind java的jar文件+Android显示gif图片

    一.引言 在xamarin开发的时候,有时我们想要做一个功能,但是这个功能已经有人用java写好了,并且打包成了jar文件.那么我们可以直接把对方的jar文件拿过来用而不是重新用c#写代码. 关于bi ...

  6. Atitit 手机图片备份解决方案attilax总结

    Atitit 手机图片备份解决方案attilax总结 1.1. 图片分类 相机图片与app图片1 1.2. 增量备份,只能使用按照时间法备份..1 1.3. 备份工具选型1 1.4. App图片,只好 ...

  7. Atitit. html 使用js显示本地图片的设计方案.doc

    Atitit. html 使用js显示本地图片的设计方案.doc 1.  Local mode  是可以的..web模式走有的不能兰.1 2. IE8.0 显示本地图片 img.src=本地图片路径无 ...

  8. vue项目中遇到的打印,以及处理重新排版后不显示echarts图片问题。

    1. 项目中用到的打印 页面: css: 控制好宽度一般A4 我调试的是794px多了放不下,小了填不满.当时多页打印的时候,一定要控制好每一个页面内容显示的高度不要超过一个页面,当然根据自己项目来. ...

  9. MFC对话框显示BMP图片

    1.MFC对话框显示BMP图片我们先从简单的开始吧.先分一个类: (一) 非动态显示图片(即图片先通过资源管理器载入,有一个固定ID) (二) 动态载入图片(即只需要在程序中指定图片的路径即可载入) ...

随机推荐

  1. React后台管理手动封装图片上传组件

    分为两个文件夹,index.js(逻辑文件) styled.js(样式文件) index.js文件,编写完成之后在对应的地方引入即可 import React from "react&quo ...

  2. 观list.clear()方法 有感

    一 . list.clear()底层源码实现 在使用list 结合的时候习惯了 list=null :在创建这样的方式,但是发现使用list的clear 方法很不错,尤其是有大量循环的时候 1.lis ...

  3. 2014 SummerTrain Beautiful Garden

    There are n trees planted in lxhgww's garden. You can assume that these trees are planted along the ...

  4. WEB应用安全解决方案测试验证

    WEB应用安全解决方案测试报告 --- By jiang.jx at 2017-08-11 WEB应用安全解决方案.docx 链接:https://share.weiyun.com/068b05467 ...

  5. matlab批量修改变量的名称

    使用matlab做实验的时候,保存的文件里面的变量名都是一样的 ,所以希望能够把变量名全部都重命名.我举个个例子,假设我一堆文件,文件名分别是gds1,gds2,gds2,-.. 但是实际上load进 ...

  6. linux性能分析工具free

  7. 微信小程序(4)--二维码窗口

    微信小程序二维码窗口: <view class="btn" bindtap="powerDrawer" data-statu="open&quo ...

  8. 关于TVS、ESD、稳压二极管、压敏电阻

    一.稳压管和TVS管的工作原理 稳压二极管(又叫齐纳二极管),是一种直到临界反向击穿电压前都具有很高电阻的半导体器件,在这临界击穿点上,反向电阻降低到一个很小的数值,在这个低阻区中电流增加而电压则保持 ...

  9. [AtCoder] Yahoo Programming Contest 2019

    [AtCoder] Yahoo Programming Contest 2019   很遗憾错过了一场 AtCoder .听说这场是涨分场呢,于是特意来补一下题. A - Anti-Adjacency ...

  10. ubuntu 解压zip 文件乱码

    用 unzip -O cp936 就可以了, 但是,有些发行版所带的 unzip 没有这个参数,比如 ArchLinux 就需要安装 unzip-iconv. Ubuntu 12.04 里面的 unz ...