【问题标题】:DrawView save combined image (multiply)DrawView 保存组合图像(乘法)
【发布时间】:2016-04-08 00:35:33
【问题描述】:

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

}

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

有什么想法吗?

【问题讨论】:

    标签: ios swift uiimageview


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-15
      • 2016-07-28
      • 2022-11-20
      • 2013-03-10
      • 2014-08-15
      • 1970-01-01
      • 2016-05-19
      • 1970-01-01
      相关资源
      最近更新 更多