iOS之CATiledLayer的属性简介和使用
1、CATiledLayer简介
CATiledLayer用于大型图片进行分割显示,需要显示的图片才会加载,直接上代码:
- (void)viewDidLoad {
[super viewDidLoad];
[self cutImageAndSave];
[self addTiledLayer];
}
- (void)drawLayer:(CATiledLayer *)layer inContext:(CGContextRef)ctx{
CGRect bounds = CGContextGetClipBoundingBox(ctx);
NSInteger x = floor(bounds.origin.x / layer.tileSize.width);
NSInteger y = floor(bounds.origin.y / layer.tileSize.height);
//load tile image
NSString *filePath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
NSString *imageName = [NSString stringWithFormat:@"%@/pic-%02ld-%02ld.png",filePath,x,y];
UIImage *tileImage = [UIImage imageWithContentsOfFile:imageName];
UIGraphicsPushContext(ctx);
[tileImage drawInRect:bounds];
UIGraphicsPopContext();
}
//添加CATiledLayer
- (void)addTiledLayer{
UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(, , CScreenWidth, CScreenHeight)];
[self.view addSubview:scrollView];
UIImage *image = [UIImage imageNamed:@"pic1.jpg"];
CATiledLayer *tiledLayer = [CATiledLayer layer];
tiledLayer.frame = CGRectMake(, , image.size.width, image.size.height);
tiledLayer.delegate = self;
tiledLayer.tileSize = CGSizeMake(, );
_tiledLayer = tiledLayer;
scrollView.contentSize = tiledLayer.frame.size;
[scrollView.layer addSublayer:tiledLayer];
[tiledLayer setNeedsDisplay];
}
//切图保存到沙盒
- (void)cutImageAndSave{
NSString *filePath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
NSString *imageName = [NSString stringWithFormat:@"%@/pic-00-00.png",filePath];
UIImage *tileImage = [UIImage imageWithContentsOfFile:imageName];
NSLog(@"%@",imageName);
if (tileImage) return;
UIImage *image = [UIImage imageNamed:@"pic1.jpg"];
UIImageView *imageView = [[UIImageView alloc]initWithImage:image];
CGFloat WH = ;
CGSize size = image.size;
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:@"%@/pic-%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;
}
效果图

2、CATiledLayer属性
#import <QuartzCore/CALayer.h> NS_ASSUME_NONNULL_BEGIN CA_CLASS_AVAILABLE (10.5, 2.0, 9.0, 2.0)
@interface CATiledLayer : CALayer //初次加载淡入时间,默认0.25s
//由于是类方法,无法直接修改,创建子类进行方法覆盖就行。
+ (CFTimeInterval)fadeDuration; //这两个属性用处不太懂???
/* The number of levels of detail maintained by this layer. Defaults to
* one. Each LOD is half the resolution of the previous level. If too
* many levels are specified for the current size of the layer, then
* the number of levels is clamped to the maximum value (the bottom
* most LOD must contain at least a single pixel in each dimension). */ @property size_t levelsOfDetail; /* The number of magnified levels of detail for this layer. Defaults to
* zero. Each previous level of detail is twice the resolution of the
* later. E.g. specifying 'levelsOfDetailBias' of two means that the
* layer devotes two of its specified levels of detail to
* magnification, i.e. 2x and 4x. */ @property size_t levelsOfDetailBias; //Defaults to (256, 256),设置CATiledLayer的item的大小
@property CGSize tileSize; @end NS_ASSUME_NONNULL_END
iOS之CATiledLayer的属性简介和使用的更多相关文章
- iOS之CAGradientLayer属性简介和使用
1.CAGradientLayer简介 CAGradientLayer用于制作背景图层的颜色渐变,也就是颜色梯度!相关属性简介: #import <QuartzCore/CALayer.h> ...
- iOS开发UI篇—CALayer简介
iOS开发UI篇—CALayer简介 一.简单介绍 在iOS中,你能看得见摸得着的东西基本上都是UIView,比如一个按钮.一个文本标签.一个文本输入框.一个图标等等,这些都是UIView. 其实 ...
- (转)IOS之Info.plist文件简介
原文:IOS之Info.plist文件简介 http://www.apkbus.com/android-130240-1-1.html (出处: Android开发论坛 - 安卓开发论坛 - Andr ...
- iOS数据存储之属性列表理解
iOS数据存储之属性列表理解 数据存储简介 数据存储,即数据持久化,是指以何种方式保存应用程序的数据. 我的理解是,开发了一款应用之后,应用在内存中运行时会产生很多数据,这些数据在程序运行时和程序一起 ...
- Pop–实现任意iOS对象的任意属性的动态变化
简介 Pop 是一个可扩展的动画引擎,可用于实现任意iOS对象的任意属性的动态变化,支持一般动画,弹性动画和渐变动画三种类型. 项目主页: pop 最新示例: 点击下载 注意: 官方代码中,并不包含实 ...
- HTML5 Audio and Video 的新属性简介
前言:HTML5 中 Audio and Video的使用方法比较简单,但就是比较复杂,方法属性多.如果不常用的几乎难以记住,甚至有些人难以区分不同属性和方法的作用,更别说应用了.以下对Audio a ...
- iOS开发多线程篇—多线程简介
iOS开发多线程篇-多线程简介 一.进程和线程 1.什么是进程 进程是指在系统中正在执行的一个应用程序 每一个进程之间是独立的.每一个进程均执行在其专用且受保护的内存空间内 比方同一时候打开QQ.Xc ...
- Android Activity动画属性简介
Android Activity动画属性简介 在Android当中 设置activity的动画 需要复写 android:windowAnimationStyle这个属性 我们自定义一个动画样式来继承 ...
- iOS之CAScrollLayer属性简介和使用
1.CAScrollLayer的简介 CAScrollLayer用于显示一个滑动图层的一部分,可以确定滑动方向和可视区域面积,限制不滑出区域外!相关属性如下:其中 /* Scroll the cont ...
随机推荐
- fastReport.net 初了解
delphi 中fastReport rmReport都很好用,转到.net了,第一想法也是这两个,好在这里有个fastReport; 这个安装呢 找个破解的 有个4.x版 安完建一个winForm ...
- USACO 2009 Open Cow Line /// 队列 oj26220
题目大意: 输入n,n次操作 操作A:在L(左边)或R(右边)插入一个递增的数 操作D:在L(左边)或R(右边)删除m个数 Sample Input 10A LA LA RA LD R 2A RA R ...
- MZ头里面的东西。真他妈多
最后一个字段,也就是e_lfanew,的值表示PE头相对于文件首部的偏移,也就是说,在它的值所对应的位置,就是PE头的地址, 数据结构名称 值 e_magic: 0x5A4D->‘MZ’ e_c ...
- scrapy不抓取重复的网页解决办法
1.scrapy爬虫遇到一个问题,有时候会对一个网页重复爬取提取不同的数据,这时候会发现,后面的那个重复爬取scrapy直接终止了. 原因: scrapy 的request逻辑里面 dont_fil ...
- WIN10安装CUDA10 cuDNN
文章目录 CPU和GPU 什么是CUDA 什么是cuDNN WIN10安装CUDA10 WIN10安装cuDNN CPU和GPU CPU和GPU是不一样的计算机设备,CPU作为计算机心脏一直被人们所认 ...
- AndroidStudio WiFi调试插件
前言 此篇博客也是Android studio插件篇的一部分,后续有时间我会介绍更多AndroidStudio的插件方便开发. Android设备用WiFi调试在以前一般是通过adb连接的,但是这样的 ...
- phonegap 开发指南系列----开始之前(1)
在基于任何平台(安卓.ios等phonegap支持的平台)上做phonegap开发之前,需要安装 cordova 的 command-line interface (CLI) .CLI详细:http: ...
- 【JZOJ6345】ZYB建围墙
description analysis 打表找规律,自认为样例给的提示很明显 容易想到最优方案是让家庭尽量先围成一个正六边形,剩下的在最外层绕一个圈 手推一波可以知道,如果正六边形有\(n\)层,剩 ...
- Open CV 环境配置
{ //https://github.com/zhmmmm/ANYTOOL-2.0.0.0.2Version/tree/master/OpenCVProject } /* //各个版本下载 https ...
- Helvetic Coding Contest 2018 online mirror (teams allowed, unrated)F3 - Lightsabers (hard)
题意:n个数字1-m,问取k个组成的set方案数 题解:假设某个数出现k次,那么生成函数为\(1+x+...+x^k\),那么假设第i个数出现ai次,结果就是\(\sum_{i=1}^m(1+x+.. ...