Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/99.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios DrawView保存组合图像(倍增)_Ios_Swift_Uiimageview - Fatal编程技术网

Ios DrawView保存组合图像(倍增)

Ios DrawView保存组合图像(倍增),ios,swift,uiimageview,Ios,Swift,Uiimageview,我有两个UIImageView,一个在底部,在第二个UIImageView上显示默认图像(如照片),您可以在其中绘制 我想从两个图像创建一个UIImage,并将其另存为新图像 我该怎么做?我曾尝试过: func saveImage() { print("save combined image") let topImage = self.canvasView.image let bottomImage = self.backgroundImageView.image

我有两个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)

}
但结果不正确(拉伸且无覆盖)


有什么想法吗?

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

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