【问题标题】:Save photo in square format以方形格式保存照片
【发布时间】:2019-01-21 05:18:11
【问题描述】:

我想在照片库中保存一张完美正方形的照片。这延伸到屏幕的整个宽度。我下面的代码将图像定位在正方形中,但我不知道如何使位置完美。我只是发布面具部分的代码。我没有把我的整个代码。我在保存图像时没有问题。

@IBAction func mask(_ sender: Any) {
    let bottomImage:UIImage = UIImage(named: "backdropd")!
    let newSize2 = CGSize(width: bottomImage.size.width, height: bottomImage.size.height)
    UIGraphicsBeginImageContextWithOptions(newSize2, false, bottomImage.scale)

    if  let rightz:UIImage = rightImage.image{
        rightz.draw(in: CGRect(x: newSize2.width * 0.125,y: newSize2.height * 0.25,width: newSize2.width/1,height:   newSize2.height/2), blendMode:CGBlendMode.normal, alpha:1.0)
    }

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

    fullImage.image = newImage
}

【问题讨论】:

  • 除非图片不是完美的正方形,否则两种情况会出现width>height(风景图片),width宽度,在这种情况下,如果你想创建一个正方形,你可以通过 3 种方式裁剪图像,1. 取顶部正方形部分,2. 取中间正方形部分,3 . 取底部正方形部分。你想把哪个部分当作正方形?
  • 您是否希望您的rightImage 为正方形并以bottomImage 为中心?
  • @RatulSharker 我想要肖像,但我不想裁剪图像。但是,如果必须,我希望它从中心裁剪。
  • 如果您不想裁剪,则必须有一个间隙,您是想用静态颜色填充该间隙还是从中心裁剪。
  • @RatulSharker 我想用单色红色填充颜色。

标签: ios swift uiimage cgrect uigraphicscontext


【解决方案1】:

首先,您必须从原始图像中切出一个正方形。假设图像是landscape 模式图像。所以width > height。所以生成的正方形图像大小将是height * height。然后根据您的需要绘制图像,背景颜色为红色。

landscape 图像完成以下代码。对于potrait 图像,任务是相同的。

let image = UIImage(named: "image")!

        if image.size.width > image.size.height {
            // assuming the image is landscape one
            let sqrImgSize = image.size.height
            let extraPortion = (image.size.width - sqrImgSize) / 2
            let sqrPortionRect = CGRect(x: extraPortion,
                                        y: 0,
                                        width: sqrImgSize,
                                        height: sqrImgSize)


            // first of all, create the square image by cropping left right side out
            UIGraphicsBeginImageContextWithOptions(CGSize(width: sqrImgSize, height: sqrImgSize), false, 0.0);
            image.draw(at: CGPoint(x: -extraPortion, y: 0))

            let sqrImage = UIGraphicsGetImageFromCurrentImageContext(); // here is your center cropped image
            UIGraphicsEndImageContext();


            // now if you are interested in filling the background with red, here it is
            UIGraphicsBeginImageContextWithOptions(image.size, false, 0.0);
            let context = UIGraphicsGetCurrentContext();
            context?.beginPath()
            context?.setFillColor(UIColor.red.cgColor)
            context?.move(to: .zero)
            context?.addRect(CGRect(origin: .zero, size: image.size))
            context?.closePath()
            context?.fillPath()
            sqrImage!.draw(at: CGPoint(x: extraPortion, y: 0))
            let sqrImageWithRedBackground = UIGraphicsGetImageFromCurrentImageContext();
            UIGraphicsEndImageContext();

            imageView.image = sqrImageWithRedBackground
        } else {
            // do as above with slight calculation changes
        }

示例 这里是带有黄色背景的图像视图,其中内容模式设置为宽高比合适。所以图像视图的背景颜色是可见的,因为纵横比不匹配。

我的输出

说明 很明显,紫色是UIViewcontrollerview 属性的背景颜色。黄色是UIImageView 的背景色。红色部分是填充颜色,位于图像中心。

希望它能达到你的目的。

【讨论】:

  • 伟大的工作 Ratul。如果我想将图像稍微向右移动 50 像素作为中心,我该怎么做?
  • 将 50 添加到 extraPortion 此处 sqrImage!.draw(at: CGPoint(x: extraPortion, y: 0))
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-12-24
  • 1970-01-01
  • 2014-01-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多