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. sails中创建和使用services

    从sails官方在线文档查知 // EmailService.js - in api/services module.exports = { sendInviteEmail: function(opt ...

  2. Codeforces Round #525 D - Ehab and another another xor problem /// 构造

    题目大意: 本题有两个隐藏起来的a b(1<=a,b<=1e30) 每次可 printf("? %d %d\n",c,d); 表示询问 a^c 与 b^d 的相对大小 ...

  3. shell对比两个目录中有哪些文件不同

    参考:https://www.cnblogs.com/liyuchuan/p/10756066.html 目录结构: /data/dir1 subdir/ subfile /data/dir2 sub ...

  4. logback日志文件的使用

    1.引入Jar包,Maven pom.xml <!-- Logging with SLF4J & LogBack --> <dependency> <groupI ...

  5. final修饰和StringBuffer的几个案例(拼接,反转,对称操作)

    final关键字修饰时如果是基本数据类型的变量,则其数值一旦在初始化之后便不能更改:如果是引用类型的变量,则在对其初始化之后便不能再让其指向另一个对象,但引用变量不能变,引用变量所指向的对象中的内容还 ...

  6. kubernetes监控和性能分析工具:heapster+influxdb+grafana

    1.部署heapster 下载 heapster 相关 yaml 文件 [root@master dashboard]# wget https://raw.githubusercontent.com/ ...

  7. 使用C++网络库libcurl

    libcurl使用问题记录 libcurl库的初始化和关闭 curl_global_init()和curl_global_cleanup().这两个函数并不是线程安全的.所以只能在主线程中进行一次的初 ...

  8. PSCC2019常用基础操作

    一.常用设置 1.界面设置(快捷键Ctrl+K):可以对PS界面的颜色.导出格式.性能等等进行设置(这里暂存盘建议设置D盘或F盘,默认C盘). 2.常用面板整理(菜单栏->窗口) 二.常用快捷键 ...

  9. 巧用android:divider属性设置LinearLayout中元素之间的间隔

    如上图,要想实现3个button线性排列并且使它们的大小相同.间隔相等.而且整体填充满整个linearlayout,我们一般的做法是在每两个button之间放一个固定宽度的view,然后设置butto ...

  10. idea运行tomcat,控制台中文乱码

    加入参数:-Dfile.encoding=UTF-8