0

我想创建一个 ImageView 对象,但是,当我使用 contentRect 属性创建时,结果有任何阴影,帮帮我。

看这段代码:

CALayer *blueLayer = [CALayer layer];
blueLayer.frame = CGRectMake(50.0f, 50.0f, 100.0f, 200.0f);
blueLayer.backgroundColor = [UIColor blueColor].CGColor;
UIImage *image = [UIImage imageNamed:@"vclrs_me_wodeshoucang"] ;
CGImageRef imageRef = image.CGImage ;
blueLayer.contents = (__bridge id _Nullable)imageRef ;
blueLayer.contentsGravity =  kCAGravityCenter ;
blueLayer.contentsRect = CGRectMake(0, 0, 2, 2) ;
blueLayer.contentsScale = image.scale ;
[self.layerView.layer addSublayer:blueLayer];

并且,请阅读结果(图片): 在此处输入图像描述

4

2 回答 2

0

尝试设置 self.layerView.clipsToBounds = YES

于 2016-12-08T09:22:16.777 回答
0

https://developer.apple.com/reference/quartzcore/calayer/1410866-contentsrect?language=objc

根据上述文件

默认为单位矩形 (0.0, 0.0, 1.0, 1.0)。

如果请求单位矩形之外的像素,则内容图像的边缘像素将向外扩展。

如果提供了一个空矩形,则结果是未定义的。

所以根据它它会给出这样的输出

图片

于 2016-12-08T08:53:33.873 回答