保存图像视图上有另一个蒙版的ImageView吗?

时间:2017-04-19 05:24:08

标签: ios swift

enter image description here 我的视图层次结构

包含的ContentView 背景ImageView 和前ImageView

我想要做的是,只要我的视图获得加载,即调用viewDidLoad()我正在前面的ImageView上应用遮罩。像这样:

func viewDidLoad() {

  maskLayer.frame = frontImageView.bounds
        maskLayer.shadowRadius = 5
        maskLayer.shadowPath = CGPath(roundedRect: frontImageView.bounds.insetBy(dx: CGFloat(5), dy: CGFloat(5)), cornerWidth: 10, cornerHeight: 10, transform: nil)
        maskLayer.shadowOpacity = 15
        maskLayer.shadowOffset = CGSize.zero
        maskLayer.shadowColor = UIColor.white.cgColor
        frontImageView.layer.mask = maskLayer 

}

然后我想要的是保存我得到的图像,我这样做:

我正在调用此方法来获取图像:

 func returnFinalImage() -> UIImage {
        UIGraphicsBeginImageContextWithOptions(backGroundImageView.frame.size, backGroundImageView.isOpaque, 0.0)
        contentView.layer.render(in: UIGraphicsGetCurrentContext()!)
        let finalImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return finalImage!
    }

但是当图像保存到照片库时,我得到的是一张没有图像的空白画布。

我希望我能够表达自己。 任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:2)

我想出了怎么做。以下是代码:

/var/folders/r8/29j5d4f15csc0x727z1y6zr8000086/T/hudson5065016463950292533.sh: line 5: which: command not found
/var/folders/r8/29j5d4f15csc0x727z1y6zr8000086/T/hudson5065016463950292533.sh: line 6: which: command not found
/var/folders/r8/29j5d4f15csc0x727z1y6zr8000086/T/hudson5065016463950292533.sh: line 7: which: command not found
/var/folders/r8/29j5d4f15csc0x727z1y6zr8000086/T/hudson5065016463950292533.sh: line 8: whoami: command not found
/var/folders/r8/29j5d4f15csc0x727z1y6zr8000086/T/hudson5065016463950292533.sh: line 9: whoami: command not found
whoami = 
./setup-git-svn-repo.sh: line 3: dirname: command not found

答案 1 :(得分:0)

您正在从contentView的图层进行渲染,而您的代码中没有其他地方提及contentView。

我建议更换

contentView.layer.render(in: UIGraphicsGetCurrentContext()!)

frontImageView.layer.render(in: UIGraphicsGetCurrentContext()!)