gpt4 book ai didi

ios - DrawView保存组合图(相乘)

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:01:19 28 4
gpt4 key购买 nike

我有 2 个 UIImageView - 一个在底部并显示默认图像(如照片)- 在第二个 UIImageView 上您可以在其中绘制。

我想从两个图像创建一个 UIImage,并将其保存为新图像。

我该怎么做?我试过:

 func saveImage() {

print("save combined image")

let topImage = self.canvasView.image
let bottomImage = self.backgroundImageView.image

let size = CGSizeMake(topImage!.size.width, topImage!.size.height)
UIGraphicsBeginImageContextWithOptions(size, false, 0.0)

[topImage!.drawInRect(CGRectMake(0,0,size.width, topImage!.size.height))];
[bottomImage!.drawInRect(CGRectMake(0,0,size.width, bottomImage!.size.height))];

let newImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()

UIImageWriteToSavedPhotosAlbum(newImage, self, #selector(CanvasViewController.image(_:didFinishSavingWithError:contextInfo:)), nil)

}

但结果不正确(拉伸(stretch)且没有叠加)

有什么想法吗?

最佳答案

好的,我找到了一个解决方案,只需要添加“乘法”作为混合模式。

    let topImage = self.canvasView.image
let bottomImage = self.backgroundImageView.image

let size = CGSizeMake(bottomImage!.size.width, bottomImage!.size.height)
UIGraphicsBeginImageContextWithOptions(size, false, 0.0)


[bottomImage!.drawInRect(CGRectMake(0,0,size.width, bottomImage!.size.height))];

[topImage!.drawInRect(CGRectMake(0,0,size.width, bottomImage!.size.height), blendMode: CGBlendMode.Multiply , alpha: 1.0)];

let newImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()

关于ios - DrawView保存组合图(相乘),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36489587/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com