iOS

气泡图片

给ImageView加Layer

Posted by Alpaca on November 3, 2016

准备一张这种气泡效果的背景图,然后将这个气泡图做成一个layer实例,并且通过contentCenter或者contentRect拉伸至我们需要展示的UIImageView大小

将做好的layer实例赋给UIImageView

将imageview赋上图片

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
CGRect frame = CGRectMake(20, 100, 300, 300);

UIImageView *image = [[UIImageView alloc] initWithFrame:frame];

CAShapeLayer *layer = [CAShapeLayer layer];

layer.frame = image.bounds;
layer.contents = (id)[UIImage imageNamed:@"chat.png"].CGImage;
layer.contentsCenter = CGRectMake(0.5, 0.5, 0.1, 0.1);
layer.contentsScale = [UIScreen mainScreen].scale;

image.layer.mask = layer;
image.layer.frame = image.frame;
image.image = [UIImage imageNamed:@"1.jpg"];
[self.view addSubview:image];

Like this ⤵️:

文章使用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议