如何将图像放置在 imageView 后面

发布于 2024-11-01 17:01:02 字数 1477 浏览 0 评论 0原文

我有一个包含多个图层的图像。用户可以将图片设置为图层。但是,当没有可用图片时,我想要一张替换图片。

我想我有两个选择:

  1. 检查图像(拍摄的照片)是否已加载到我的目录中。如果没有放置其他图像。
  2. 将另一个图像 (photo_icon.png) 放置到 UIImageView (image1) 的后面。当未拍摄照片时,图像变得可见。

这是到目前为止我的代码的片段。

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];

将 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 ];

提前致谢!

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). 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:

    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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暖风昔人 2024-11-08 17:01:02

您可以使用不透明度来显示/隐藏图像。

you can use opacity for the image to show/hide.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文