简体   繁体   English

如何将图像放在imageView后面

[英]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: 我认为我有两种选择:

  1. check if the image (taken photo) is loaded into my directory. 检查图像(拍摄的照片)是否已加载到我的目录中。 if not place the other image. 如果没有放置其他图像。
  2. placing the other image (photo_icon.png) to the back of the UIImageView (image1). 将另一个图像(photo_icon.png)放在UIImageView(image1)的背面。 When a photo hasn't been taken the image becomes visible. 未拍摄照片时,图像可见。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM