离屏渲染的基础知识整理

简介

最近在看一些离屏渲染的东西,接触到一些新的名词,轻量级别的 CALayer 代替 UIView ,mask ,bitmap 整理下这部分的内容

UIView

我们先来看看 UIView 的说明

@interface UIView : UIResponder 

UIView 继承了UIResponder,所以 UIView 可以响应用户的一些操作。而且 UIView 遵循代理 CALayerDelegate 。和 CALayer 有了一点点的联系

layer属性

@property(nonatomic, readonly, strong) CALayer *layer;

Discussion

This property is never nil. The actual class of the object is determined by the value returned in the layerClass property. The view is the layer’s delegate.

Warning
Because the view is the layer’s delegate, never make the view the delegate of another CALayer object. Additionally, never change the delegate of this layer object.

从文档中可以看出的是,每一个 view.layer都不是 nil,而且这个 view 是 layer 的 delegate。而且永远不要去改变 这个 layer 的代理

CALayer

@interface CALayer : NSObject 

CALayer继承于 NSObject ,无法响应用户的操作。

Overview

A layer’s main job is to manage the visual content that you provide but the layer itself has visual attributes that can be set, such as a background color, border, and shadow. In addition to managing visual content, the layer also maintains information about the geometry of its content (such as its position, size, and transform) that is used to present that content onscreen.
If the layer object was created by a view, the view typically assigns itself as the layer’s delegate automatically, and you should not change that relationship.

从 Overview 中截取了一部分,layer 的首要任务就是管理你提供的那些内容,当然它本身也可以设置一些显示的属性,比如 color、border、shadow 等。当然 layer 也帮助你管理显示在屏幕的内容。像是位置、大小和一些变换的东西

layer 中一些触发离屏渲染的属性

mask

@property(strong) CALayer *mask;

The layer’s alpha channel determines how much of the layer’s content and background shows through. Fully or partially opaque pixels allow the underlying content to show through but fully transparent pixels block that content.

mask 也是一个 CALayer, mask 代表着怎么样的像素能够穿过 mask 被显示出来。你一定写过这样的代码。

 imageView.image.layer.cornerRadius = 5;
 imageView.image.layer.masksToBounds = YES;

我们来分别看看这两行代码代表什么,设置圆角,然后进行裁剪显示出来圆角

1.cornerRadius

Setting the radius to a value greater than 0.0 causes the layer to begin drawing rounded corners on its background. By default, the corner radius does not apply to the image in the layer’s contents property; it applies only to the background color and border of the layer. However, setting the masksToBounds property to YES causes the content to be clipped to the rounded corners.

划重点,在默认情况下,cornerRadius 不会对 layer 的内容是 image 生效的,但是只要你设置 masksToBounds 为 YES,那么这样就是可行的

2.masksToBounds

When the value of this property is YES, Core Animation creates an implicit clipping mask that matches the bounds of the layer and includes any corner radius effects. If a value for the mask property is also specified, the two masks are multiplied to get the final mask value.
如果设置了masksToBounds 为 YES,那么 Core Animation 就会隐式的创建一个 与 layer 的 bounds 相匹配的并且裁切过的 mask,这个 mask 就可以实现圆角的效果

shadow
1. shadowOpacity

The opacity of the layer’s shadow. Animatable.
设置 layer 的不透明度

2.shadowRadius

The blur radius (in points) used to render the layer’s shadow. Animatable.
用来进行阴影半径渲染的模糊半径

3. shadowOffset

The offset (in points) of the layer’s shadow. Animatable.
设置 layer 的阴影偏移量

edgeAntialiasing
edgeAntialiasingMask

This property specifies which edges of the layer are antialiased and is a combination of the constants defined in CAEdgeAntialiasingMask. You can enable or disable antialiasing for each edge (top, left, bottom, right) separately. By default antialiasing is enabled for all edges.
通过这个属性可以对某一个或者某几个边启用或者不启用抗锯齿,当然默认是对所有的生效

allowsEdgeAntialiasing

When the value is YES, the layer is allowed to antialias its edges, as requested by the value in the layer’s edgeAntialiasingMask property. The default value is read from the boolean UIViewEdgeAntialiasing property in the main bundle’s Info.plist file. If no value is found, the default value is NO.
开启了抗锯齿的话,就回去访问edgeAntialiasingMask,看看会有那些边开启抗锯齿

group opacity

allowsGroupOpacity
@property BOOL allowsGroupOpacity;

When the value is YES and the layer’s opacity property value is less than 1.0, the layer is allowed to composite itself as a group separate from its parent. This gives correct results when the layer contains multiple opaque components, but may reduce performance.
如果设置 allowsGroupOpacity为 YES 并且 layer 的opacity小于1,那么该layer将 被允许将其自身合并为一个组,并且是与他的parent分开的。而且需要注意的是,如果这个 layer 还包含很多的不透明的子 layer 的话,那么就会牺牲一些性能。而在 iOS 7+ ,这个属性默认为 YES 。

bitmap

layer 中有个属性 contents

Declaration

@property(strong) id contents;
Discussion

If you are using the layer to display a static image, you can set this property to the CGImageRefcontaining the image you want to display. (In macOS 10.6 and later, you can also set the property to an NSImage Assigning a value to this property causes the layer to use your image rather than create a separate backing store.

contents 可以设置一个静态的图片,传入一个CGImageRef。打开这个CGImageRef会发现这样的一段描述

CGImageRef

Structure
A bitmap image or image mask.

 typedef struct CF_BRIDGED_TYPE(id) CGImage *CGImageRef;

这是个指向 CGImage 结构体的指针

CGImage

A bitmap (or sampled) image is a rectangular array of pixels, with each pixel representing a single sample or data point in a source image.
一个 bitmap image 就是一个矩形像素阵列,每个像素表示源图像中的单个样本或数据点。

总结

最近在接触一些离屏渲染的东西,看到这些陌生的名词。先动手整理了下,方便以后查看。先写到这里,各位大佬轻拍

你可能感兴趣的:(离屏渲染的基础知识整理)