1、CALayer 有一个属性叫做contents,这个属性的类型被定义为id,意味着它可以是任何类型的对象。在这种情况下,你可以给contents属性赋任何值,你的app仍然能够通过编译。但是,在实践中,如果你给contents赋的不是CGImage,那么你得到的图层将是空白的。
2、通过一个实例给图层的contents赋值一张图片
//
// ViewController.m
// 002-layerView
//
// Created by freedom on 2016/12/15.
// Copyright © 2016年 Revan. All rights reserved.
//
#import "ViewController.h"
//#import <QuartzCore/QuartzCore.h>
@interface ViewController ()
/**
* layerView
*/
@property (nonatomic, strong) UIView *layerView;
/**
* blueLayer
*/
//@property (nonatomic, strong) CALayer *blueLayer;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 背景颜色
self.view.backgroundColor = [UIColor darkGrayColor];
// 图片
UIImage *img = [UIImage imageNamed:@"img"];
// View
self.layerView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 200, 200)];
self.layerView.backgroundColor = [UIColor whiteColor];
// 给视图的图层设置图片
self.layerView.layer.contents = (__bridge id _Nullable)(img.CGImage);
self.layerView.contentMode = UIViewContentModeScaleAspectFit;
//设置视图的模式
self.layerView.layer.contentsGravity = kCAGravityCenter;
// self.layerView.layer.contentsScale = img.scale;//[UIScreen mainScreen].scale;
//图层内容放大
// self.layerView.layer.contentsScale = 0.5;
[self.view addSubview:self.layerView];
//blueLayer
// self.blueLayer = [CALayer layer];
// self.blueLayer.frame = CGRectMake(50, 50, 100, 100);
// self.blueLayer.backgroundColor = [UIColor blueColor].CGColor;
// [self.layerView.layer addSublayer:self.blueLayer];
}
@end
3、属性contentGravity
CALayer与 contentMode 对应的属性叫做 contentsGravity,但是它是一个NSString类型,而不是像对应的UIKit部分,那里面的值是枚举。contentsGravity 可选的常量有一下一些:
kCAGravityCenter
kCAGravityTop
kCAGravityBottom
kCAGravityLeft
kCAGravityRight
kCAGravityTopLeft
kCAGravityTopRight
kCAGravityBottomLeft
kCAGravityBottomRight
kCAGravityResize
kCAGravityResizeAspect
kCAGravityResizeAspectFill
和contentmode一样,contentsGravity的目的是为了决定内容在图层的边界中怎么对齐,我们将使用kCAGravityResizeAspect,它的效果等同于UIViewContentModeScaleAspectFit,同时它还能在图层等比例拉伸以适应图层的边界
//设置视图图层的模式
self.layerView.layer.contentsGravity = kCAGravityCenter;
4、maskToBounds
- 1、UIView有一个叫做 clipsToBounds 的属性可以用来决定是否显示超出边界的内容
- 2、CALayer对应的属性叫做maskTobounds,把它设置为YES,就会切除超出部分
5、contentsRect
- 1、CALayer的contentsRect 属性允许我们在图层边框里显示寄宿图的一个子域。这涉及到图片是如何显示和拉伸的,所以要比contentsGravity灵活多了
- 2、和bounds,frame不同,contentsRect不是按点来计算的,它使用了单位坐标,单位坐标指定在0~1之间,是一个相对值(像素和点就是绝对值),所以是相对与寄宿图的尺寸的。
- 3、默认的contentsRect 是{0, 0, 1, 1},这意味着整个寄宿图默认都是可见的,如果我们指定一个小一点的矩形,图片就会别裁剪
- 4、给contentsRect设置一个负数的原点 或者是 设置尺寸大于{1,1}的尺寸也是可以的,这种情况下,最外面的像素会被拉伸以填充剩下的区域
- 5、contentsRect 在app中最有趣的地方在于一个叫做image sprites(图片拼合)的用法
- 6、典型地,图片拼合后可以打包整合到一张大图上一次性载入。相比多次载入不同的图片,这样做能够带来很多方面的好处:
- 内存使用
- 载入时间
- 渲染性能
contentsRect实例
原图
#import "ViewController.h"
//#import <QuartzCore/QuartzCore.h>
@interface ViewController ()
/**
* iglooView
*/
@property (nonatomic, strong) UIView *iglooView;
/**
* coneView
*/
@property (nonatomic, strong) UIView *coneView;
/**
* anchorView
*/
@property (nonatomic, strong) UIView *anchorView;
/**
* shipView
*/
@property (nonatomic, strong) UIView *shipView;
/**
* blueLayer
*/
@property (nonatomic, strong) CALayer *blueLayer;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 背景颜色
self.view.backgroundColor = [UIColor darkGrayColor];
//iglooView
self.iglooView = [[UIView alloc] initWithFrame:CGRectMake(10, 100, 100, 100)];
self.iglooView.backgroundColor = [UIColor redColor];
[self.view addSubview:self.iglooView];
//coneView
self.coneView = [[UIView alloc] initWithFrame:CGRectMake(120, 100, 100, 100)];
self.coneView.backgroundColor = [UIColor blueColor];
[self.view addSubview:self.coneView];
//anchorView
self.anchorView = [[UIView alloc] initWithFrame:CGRectMake(10, 210, 100, 100)];
self.anchorView.backgroundColor = [UIColor orangeColor];
[self.view addSubview:self.anchorView];
//shipView
self.shipView = [[UIView alloc] initWithFrame:CGRectMake(120, 210, 100, 100)];
self.shipView.backgroundColor = [UIColor yellowColor];
[self.view addSubview:self.shipView];
// 图片
UIImage *img = [UIImage imageNamed:@"hc"];
[self addSpriteImage:img withContentRect:CGRectMake(0, 0, 0.5, 0.5) toLayer:self.iglooView.layer];
[self addSpriteImage:img withContentRect:CGRectMake(0.5, 0, 0.5, 0.5) toLayer:self.coneView.layer];
[self addSpriteImage:img withContentRect:CGRectMake(0, 0.5, 0.5, 0.5) toLayer:self.anchorView.layer];
[self addSpriteImage:img withContentRect:CGRectMake(0.5, 0.5, 0.5, 0.5) toLayer:self.shipView.layer];
}
// 拼合图片的方法
- (void)addSpriteImage:(UIImage *) image withContentRect:(CGRect) rect toLayer:(CALayer *) layer {
// 设置图层contents
layer.contents = (__bridge id _Nullable)(image.CGImage);
// 设置图片显示模式
layer.contentsGravity = kCAGravityResizeAspect;
// set contentsRect
layer.contentsRect = rect;
// layer.contentsCenter = rect;
}