【问题标题】:UIImage (Frame) and UIImage (Picture) mergeUIImage (Frame) 和 UIImage (Picture) 合并
【发布时间】:2018-02-10 14:48:18
【问题描述】:

我有多种尺寸的帧,可以硬编码,或者由服务器决定。我必须从 Gallery 中选择 Image,它肯定可以是多个维度的。

  1. 我正在从图库中选择图片
  2. 我正在使用代码生成白色背景 UIImage。

    让 size = CGSize(宽度: 424/2, 高度: 664/2)

      UIGraphicsBeginImageContextWithOptions(size, true, 0)
      UIColor.white.setFill()
      UIRectFill(CGRect(x: 0, y: 0, width: size.width, height: size.height))
      let background_image: UIImage? = UIGraphicsGetImageFromCurrentImageContext()
      UIGraphicsEndImageContext()
    

现在,我想要制作另一个图像,它保持领先 20 像素、前 20 像素以及宽度和高度比原始背景小 20 像素。

我怎样才能实现它。

在来到 StackOverflow 之前我尝试过的事情。

func mergedImageWith(frontImage:UIImage?, backgroundImage: UIImage?) -> UIImage{

    if (backgroundImage == nil) {
      return frontImage!
    }

    let size = CGSize(width: 424/2, height: 664/2)

    UIGraphicsBeginImageContextWithOptions(size, true, 0)
    UIColor.white.setFill()
    UIRectFill(CGRect(x: 0, y: 0, width: size.width, height: size.height))
    let backgroundImage2: UIImage? = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    UIGraphicsBeginImageContextWithOptions(size, false, 0.0)
    backgroundImage2?.draw(in: CGRect.init(x: 0, y: 0, width: size.width, height: size.height))

    frontImage?.draw(in: getAspectFillFrame(sizeImageView: size2, sizeImage: (frontImage?.size)!))

    let newImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()!
    UIGraphicsEndImageContext()

    return newImage
  }

这里的背景图像是使用方面填充创建的,但问题是起始位置以及完整的高度和宽度。

用非常简单的话来说。这就像制作自定义框架并将它们与图像(方面填充)合并以进行打印。

谁能帮帮我

谢谢。

【问题讨论】:

    标签: ios uiimage uigraphicscontext


    【解决方案1】:

    在绘制完所有图像之前不要结束你的图像上下文(我还包括一些我正在工作的代码,稍微编辑了一下)

    class layeredImageView: UIImageView {
    var imageBackground:UIImage!
    var imageForeground:UIImage!
    
    UIGraphicsBeginImageContextWithOptions(self.frame.size, false, UIScreen.main.scale)
    
        self.image?.draw(in: self.frame)
    
        imageBackground.draw(in: CGRect(<rect>)
        imageForeground.draw(in: CGRect(<rect>)
    
        self.image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
    
    
    }
    

    【讨论】:

    • 问题是正确的,它不适合我想要的,你能看到这个应用程序。 lalalab.com/prints
    • 我只有两张图片,1. Frame,2. User Image。
    • 我实际上是使用上面的代码(示例中未包含该部分)来生成拼贴画(我遍历一组图像并将它们放置在 imageView 图像的特定部分)。我在 imageview 子类中使用它的原因是我可以使用 imageView 布置的任何框架来进行计算。因此,如果它在一个屏幕上为 120 像素,而在另一个屏幕上为 150 像素,其他图像就会知道信息和事物是否正确绘制。
    • 我怎样才能使它成为填充? imageForeground.draw(in: frame2)
    猜你喜欢
    • 1970-01-01
    • 2015-06-16
    • 2012-12-16
    • 2015-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-29
    • 2010-09-20
    相关资源
    最近更新 更多