@implementation MaskLayerViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self maskView];
}
//maskLayer
-(void)maskView{
CALayer * maskLayer = [CALayer layer];
maskLayer.frame = self.maskImage.bounds;
UIImage * image = [UIImage imageNamed:@“time.png”];
maskLayer.contents = (__bridge id _Nullable)(image.CGImage);
self.maskImage.layer.mask = maskLayer;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
放大和缩小过滤的算法属性
/* The filter types to use when rendering the `contents’ property of
* the layer. The minification filter is used when to reduce the size
* of image data, the magnification filter to increase the size of
* image data. Currently the allowed values are `nearest’ and `linear’.
* Both properties default to `linear’. */
@property(copy) NSString *minificationFilter;
@property(copy) NSString *magnificationFilter;
/* The bias factor added when determining which levels of detail to use
* when minifying using trilinear filtering. The default value is 0.
* Animatable. */
@property float minificationFilterBias;
算法如下
/** Contents filter names. **/
CA_EXTERN NSString * const kCAFilterNearest
CA_AVAILABLE_STARTING (10.5, 2.0, 9.0, 2.0);
CA_EXTERN NSString * const kCAFilterLinear
CA_AVAILABLE_STARTING (10.5, 2.0, 9.0, 2.0);
/* Trilinear minification filter. Enables mipmap generation. Some
* renderers may ignore this, or impose additional restrictions, such
* as source images requiring power-of-two dimensions. */
CA_EXTERN NSString * const kCAFilterTrilinear
CA_AVAILABLE_STARTING (10.6, 3.0, 9.0, 2.0);
默认的过滤器都是kCAFilterLinear,这个过滤器采用双线性滤波算法,它在大多数情况下都表现良好。双线性滤波算法通过对多个像素取样最终生成新的值,得到一个平滑的表现不错的拉伸。但是当放大倍数比较大的时候图片就模糊不清了。
kCAFilterTrilinear和kCAFilterLinear非常相似,大部分情况下二者都看不出来有什么差别。但是,较双线性滤波算法而言,三线性滤波算法存储了多个大小情况下的图片(也叫多重贴图),并三维取样,同时结合大图和小图的存储进而得到最后的结果
kCAFilterNearest是一种比较武断的方法。从名字不难看出,这个算法(也叫最近过滤)就是取样最近的单像素点而不管其他的颜色。这样做非常快,也不会使图片模糊。但是,最明显的效果就是,会使得压缩图片更糟,图片放大之后也显得块状或是马赛克严重。
DEMO如下:
//
// TensileFilterViewController.m
// LayerStudyDemo
//
// Created by apple on 2017/9/28.
// Copyright © 2017年 ZY. All rights reserved.
//
#import “TensileFilterViewController.h”
@interface TensileFilterViewController ()
@property (strong, nonatomic) IBOutletCollection(UIView) NSArray *LedViews1;
@property (strong, nonatomic) IBOutletCollection(UIView) NSArray *LedView2;
@property (nonatomic, weak) NSTimer *timer;
@end
@implementation TensileFilterViewController
{
NSArray * array;
}
- (void)viewDidLoad {
[super viewDidLoad];
array = @[self.LedViews1,self.LedView2];
UIImage *digits = [UIImage imageNamed:@“led.png”];
for (int i=0; i<array.count; i++) {
for (UIView *view in array[i]) {
//set contents
view.layer.contents = (__bridge id)digits.CGImage;
view.layer.contentsRect = CGRectMake(0, 0, 0.1, 1.0);
view.layer.contentsGravity = kCAGravityResizeAspect;
if (i==1) {
view.layer.minificationFilter = kCAFilterNearest;
}
}
}
//start timer
self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(tick) userInfo:nil repeats:YES];
}
- (void)tick
{
//convert time to hours, minutes and seconds
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierRepublicOfChina];
NSUInteger units = NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
NSDateComponents *components = [calendar components:units fromDate:[NSDate date]];
for (int i=0; i<array.count; i++) {
//set hours
[self setDigit:components.hour / 10 forView:array[i][0]];
[self setDigit:components.hour % 10 forView:array[i][1]];
//set minutes
[self setDigit:components.minute / 10 forView:array[i][2]];
[self setDigit:components.minute % 10 forView:array[i][3]];
//set seconds
[self setDigit:components.second / 10 forView:array[i][4]];
[self setDigit:components.second % 10 forView:array[i][5]];
}
}
- (void)setDigit:(NSInteger)digit forView:(UIView *)view
{
//adjust contentsRect to select correct digit
view.layer.contentsRect = CGRectMake(digit * 0.1, 0, 0.1, 1.0);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
总结
-
对于框架原理只能说个大概,真的深入某一部分具体的代码和实现方式就只能写出一个框架,许多细节注意不到。
-
算法方面还是很薄弱,好在面试官都很和蔼可亲,擅长发现人的美哈哈哈…(最好多刷一刷,不然影响你的工资和成功率???)
-
在投递简历之前,最好通过各种渠道找到公司内部的人,先提前了解业务,也可以帮助后期优秀 offer 的决策。
-
要勇于说不,对于某些 offer 待遇不满意、业务不喜欢,应该相信自己,不要因为当下没有更好的 offer 而投降,一份工作短则一年长则 N 年,为了幸福生活要慎重选择!!!
第一次跳槽十分忐忑不安,和没毕业的时候开始找工作是一样的感受,真的要相信自己,有条不紊的进行。如果有我能帮忙的地方欢迎随时找我,比如简历修改、内推、最起码,可以把烦心事说一说,人嘛都会有苦恼的~
祝大家都有美好的未来,拿下满意的 offer。