菜鸟教程小白 发表于 2022-12-11 21:53:43

ios - 以方形格式保存照片


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

<p> <a href="/image/FGs6A.png" rel="noreferrer noopener nofollow"><img src="/image/FGs6A.png" alt="image"/></a> </p>

<pre><code>@IBAction func mask(_ sender: Any) {
    let bottomImage:UIImage = UIImage(named: &#34;backdropd&#34;)!
    let newSize2 = CGSize(width: bottomImage.size.width, height: bottomImage.size.height)
    UIGraphicsBeginImageContextWithOptions(newSize2, false, bottomImage.scale)

    iflet 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
}
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>首先,您必须从原始图像中切出一个正方形。假设图像是 <code>landscape</code> 模式图像。所以 <code>width > height</code>。所以生成的正方形图像大小将是 <code>height * height</code>。然后根据您的需要绘制图像,背景颜色为红色。 </p>

<p>以下代码是为 <code>landscape</code> 图像完成的。对于 <code>potrait</code> 图像,任务是相同的。</p>

<pre><code>let image = UIImage(named: &#34;image&#34;)!

      if image.size.width &gt; 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
      }
</code></pre>

<p><strong>示例</strong>
<a href="/image/8PYt0.png" rel="noreferrer noopener nofollow"><img src="/image/8PYt0.png" alt="enter image description here"/></a>
这里是带有黄色背景的 ImageView ,其中内容模式设置为宽高比合适。所以 ImageView 的背景颜色是可见的,因为纵横比不匹配。 </p>

<p><strong>我的输出</strong>
<a href="/image/oUGI3.png" rel="noreferrer noopener nofollow"><img src="/image/oUGI3.png" alt="enter image description here"/></a> </p>

<p><strong>说明</strong>
很明显,紫色是 <code>UIViewcontroller</code> 的 <code>view</code> 属性的背景色。黄色是 <code>UIImageView</code> 的背景色。红色部分是填充颜色,位于图像中心。 </p>

<p>希望它能达到你的目的。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 以方形格式保存照片,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/54283847/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/54283847/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 以方形格式保存照片