【问题标题】:How can I place an image behind an imageView如何将图像放在 imageView 后面
【发布时间】:2011-04-15 08:11:44
【问题描述】:

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

我想我有两个选择:

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

提前致谢!

【问题讨论】:

    标签: iphone image layer


    【解决方案1】:

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

    【讨论】:

    • 好主意,但我需要知道如何检查照片是否已放置。类似 if(pngFilePath == nil){ image1.setHidden = TRUE;有什么建议吗?
    • 检查 if(img == nil){//做某事}
    • 您可以设置一个标志来检查图像是否放置。如果标志为真,则显示下一个图像,隐藏当前图像。
    • 嗯,我不明白你的解决方案@Anish,你能解释一下吗?
    • 我将图像放在 IB 中的 UIImageView 后面。当图片不在框架内时,它是透明的。放置在 IB 中的图像变得可见。
    猜你喜欢
    • 2022-10-06
    • 2019-06-20
    • 1970-01-01
    • 2012-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多