当前代码:
self.backgroundImageView.image = [self.message imageOfSize:self.message.size]; // Random image, random size
UIImage *rightBubbleBackground = [[UIImage imageNamed:@"BubbleRight"]
resizableImageWithCapInsets:BubbleRightCapInsets
resizingMode:UIImageResizingModeStretch];
CALayer *mask = [CALayer layer];
mask.contents = (id)[rightBubbleBackground CGImage];
mask.frame = self.backgroundImageView.layer.frame;
self.backgroundImageView.layer.mask = mask;
self.backgroundImageView.layer.masksToBounds = YES;
这不能正常工作。虽然应用了蒙版,但rightBubbleBackground
未正确调整大小以适合self.backgroundImageView
,即使它已设置调整大写字母插入(BubbleRightCapInsets
)。
原始图片:
掩码图像(rightBubbleBackground
):
结果:
我找到this answer但它只适用于对称图像。也许我可以修改这个答案供我使用。
答案 0 :(得分:10)
我错了。可以修改该答案以适用于不对称图像。我稍微研究了that answer并解决了我自己的问题。
以下代码使我的cap insets适用于遮罩层:
mask.contentsCenter =
CGRectMake(BubbleRightCapInsets.left/rightBubbleBackground.size.width,
BubbleRightCapInsets.top/rightBubbleBackground.size.height,
1.0/rightBubbleBackground.size.width,
1.0/rightBubbleBackground.size.height);
结果:
答案 1 :(得分:0)
即
layer.contentsScale = [UIScreen mainScreen].scale;
另外,如果您使用CAShapeLayer绘制形状并想知道它的边缘在视网膜设备上看起来有点锯齿,请尝试:
shapeLayer.rasterizationScale = [UIScreen mainScreen].scale;
shapeLayer.shouldRasterize = YES;