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. mybatis 处理枚举类型

    MyBatis支持持久化enum类型属性.假设t_user表中有一列gender(性别)类型为 varchar2(10),存储 MALE 或者 FEMALE 两种值.并且,User对象有一个enum类 ...

  2. <爬虫实战>糗事百科

    1.糗事百科段子.py # 目标:爬取糗事百科段子信息(文字) # 信息包括:作者头像,作者名字,作者等级,段子内容,好笑数目,评论数目 # 解析用学过的几种方法都实验一下①正则表达式.②Beauti ...

  3. java 数组中的数值反转输出

    package com.test; /** *数组元素反转 * */ public class ArraySwap { public static void main(String[] args) { ...

  4. robotframework冷门关键字

    1.Reload Page 模拟页面重载 2.Register Keyword To Run On Failure 参数: Keyword 描述: 当Selenium2Library类库关键字执行失败 ...

  5. drupal7权限控制之-如何访问未发表的node

    在某些特殊需求的情况下,会涉及到匿名用户或非node节点所有者,访问未发表的node节点的需求:或者需要根据不同的用户角色,访问不同的内容类型等,如果不想安装node_access等模块的时候,可以在 ...

  6. mui--使用mui中的图文表格组件时出现一条横线的解决方法

    最近做的微信公众号点击链接跳转到H5页面,该H5页面使用mui做的,遇到的商品列表页出现一个横线的问题, 这个是修改前的图片 解决方法: <style type="text/css&q ...

  7. Java 基础 - 基本类型和引用类型

    ref: https://www.cnblogs.com/ysocean/p/8482979.html#_label2 ------------------ 这里再给大家普及一个概念,在 Java 中 ...

  8. day16 python-04 字典

    Python之路,Day3 = Python基础4 # is 比较id # == 比较数值 # type(1) is type(2) 比较两个变量的类型是否相同 a = 1 b = 1 c = ' p ...

  9. thinkphp switch标签

    用法: <switch name="变量" > <case value="值1" break="0或1">输出内容1 ...

  10. POJ 3348 /// 凸包+多边形面积

    题目大意: 给定的n个点 能圈出的最大范围中 若每50平方米放一头牛 一共能放多少头 求凸包 答案就是 凸包的面积/50 向下取整 /// 求多边形面积// 凹多边形同样适用 因为点积求出的是有向面积 ...