DrawView保存组合图像(乘法)

时间:2016-04-08 00:35:34

标签: ios swift uiimageview

我有2个UIImageViews - 一个位于底部并显示默认图像(如照片) - 在第二个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)

}

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

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

Ok ill找到了解决方案,只需添加“multiply”作为混合模式。

    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()