[英]How can I place an image behind an imageView
I've got an image which contains multiple layers. 我有一个包含多层的图像。 The user can set a picture as a layer. 用户可以将图片设置为图层。 However when there isn't a picture available I'd like to have a replacement picture. 但是,当没有可用的图片时,我想替换一张图片。
I think I got two options: 我认为我有两种选择:
Here's a snippit of my code so far. 到目前为止,这是我的代码片段。
NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *pngFilePath = [NSString stringWithFormat:@"%@/photo1.png",docDir];
UIImage *img = [[UIImage alloc] initWithContentsOfFile:pngFilePath];
[image1 setImage:img];
image1.layer.cornerRadius = 15.0;
image1.layer.masksToBounds = YES;
CALayer *sublayer = [CALayer layer];
image1.layer.borderColor = [UIColor blackColor].CGColor;
image1.layer.borderWidth = 3.5;
[image1.layer addSublayer:sublayer];
CALayer *imageLayer = [CALayer layer];
imageLayer.frame = image1.bounds;
imageLayer.cornerRadius = 10.0;
imageLayer.contents = (id) [UIImage imageNamed:@"Upperlayer.png"].CGImage;
imageLayer.masksToBounds = YES;
[sublayer addSublayer:imageLayer];
replacing the image1 for the image which is needed I use: 将image1替换为我需要的图像:
CALayer *imageBackLayer = [CALayer layer];
imageBackLayer.frame = image1.bounds;
imageBackLayer.cornerRadius = 10.0;
imageBackLayer.contents = (id) [UIImage imageNamed:@"photo_icon.png"].CGImage;
imageBackLayer.masksToBounds = NO;
[sublayer insertSublayer:imageBackLayer below: image1.layer ];
Thanks in advance! 提前致谢!
您可以使用不透明度来显示/隐藏图像。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.