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的属性简介和使用的更多相关文章

  1. iOS之CAGradientLayer属性简介和使用

    1.CAGradientLayer简介 CAGradientLayer用于制作背景图层的颜色渐变,也就是颜色梯度!相关属性简介: #import <QuartzCore/CALayer.h> ...

  2. iOS开发UI篇—CALayer简介

    iOS开发UI篇—CALayer简介   一.简单介绍 在iOS中,你能看得见摸得着的东西基本上都是UIView,比如一个按钮.一个文本标签.一个文本输入框.一个图标等等,这些都是UIView. 其实 ...

  3. (转)IOS之Info.plist文件简介

    原文:IOS之Info.plist文件简介 http://www.apkbus.com/android-130240-1-1.html (出处: Android开发论坛 - 安卓开发论坛 - Andr ...

  4. iOS数据存储之属性列表理解

    iOS数据存储之属性列表理解 数据存储简介 数据存储,即数据持久化,是指以何种方式保存应用程序的数据. 我的理解是,开发了一款应用之后,应用在内存中运行时会产生很多数据,这些数据在程序运行时和程序一起 ...

  5. Pop–实现任意iOS对象的任意属性的动态变化

    简介 Pop 是一个可扩展的动画引擎,可用于实现任意iOS对象的任意属性的动态变化,支持一般动画,弹性动画和渐变动画三种类型. 项目主页: pop 最新示例: 点击下载 注意: 官方代码中,并不包含实 ...

  6. HTML5 Audio and Video 的新属性简介

    前言:HTML5 中 Audio and Video的使用方法比较简单,但就是比较复杂,方法属性多.如果不常用的几乎难以记住,甚至有些人难以区分不同属性和方法的作用,更别说应用了.以下对Audio a ...

  7. iOS开发多线程篇—多线程简介

    iOS开发多线程篇-多线程简介 一.进程和线程 1.什么是进程 进程是指在系统中正在执行的一个应用程序 每一个进程之间是独立的.每一个进程均执行在其专用且受保护的内存空间内 比方同一时候打开QQ.Xc ...

  8. Android Activity动画属性简介

    Android Activity动画属性简介 在Android当中 设置activity的动画 需要复写 android:windowAnimationStyle这个属性 我们自定义一个动画样式来继承 ...

  9. iOS之CAScrollLayer属性简介和使用

    1.CAScrollLayer的简介 CAScrollLayer用于显示一个滑动图层的一部分,可以确定滑动方向和可视区域面积,限制不滑出区域外!相关属性如下:其中 /* Scroll the cont ...

随机推荐

  1. 自学之linux的基本命令

    cd cd 用于进入指定文件夹 cd ..用于回到上个文件夹 ls ls用于列出文件夹里的所有元素 ls/home/ 列出home文件夹的元素 ls -l 可以看到文件名,拥有者是谁,什么时候修改的 ...

  2. 2018今日头条湖北省赛【A】

    [题目链接]https://www.nowcoder.com/acm/contest/104/A 这题就是很简单的几何题..md现场推了很久的cos sin仿佛像个zz.自己都想给自己一巴掌. 题意就 ...

  3. Spring MVC入门示例(1)

    1.新建一个Java Web项目 2.导入jar包 3.在WEB-INF下面建一个hello.jsp页面. 1 <%@ page language="java" import ...

  4. neo4j采坑记

    1.安装后启动不起来,解决方案: https://stackoverflow.com/questions/38607283/failed-to-start-neo4j-service  2.一直启动不 ...

  5. 2019-4-29-WPF-如何判断一个控件在滚动条的里面是用户可见

    title author date CreateTime categories WPF 如何判断一个控件在滚动条的里面是用户可见 lindexi 2019-4-29 9:42:2 +0800 2019 ...

  6. codeforces 1180E Serge and Dining Room 线段树

    题目传送门 题目大意: 给出a序列和b序列,a序列为各种食物的价格,b序列为一列排着队的小朋友拥有的钱,小朋友依次购买食物,每个人都买自己能买的起的最贵的食物,买不起就离开队伍.给出q次操作,操作1是 ...

  7. input、textarea等输入框输入中文时,拼音在输入框内会触发input事件的问题

    监听文本输入框的input事件,在拼写汉字(输入法)但汉字并未实际填充到文本框中(选词)时会触发input事件,如图: 但是在很多情况下,只需要输入到输入框的中文字符. 解决办法: 通过查阅资料得知在 ...

  8. SP1296 SUMFOUR - 4 values whose sum is 0

    传送门 解题思路 四个数组一起做有点炸.先把他们合并成两个数组,然后让一个数组有序,枚举另一个数组的元素,二分即可.时间复杂度\(O(n^2logn^2)\) 代码 #include<iostr ...

  9. [NOI 2018]冒泡排序

    题意:求所有字典序大于给定序列且满足条件的排列个数之和. 思路: 考虑dp即可,打表出卡特兰数优化,直接dp可以44... #include <bits/stdc++.h> using n ...

  10. iOS开发之SceneKit框架--SCNParametricGeometry.h

    1.SCNParametricGeometry简介 SCNParametricGeometry用于创建简单的3D模型,比如SCNPlane 平面.SCNPyramid 锥形(金字塔).SCNBox 立 ...