ios - 以方形格式保存照片

标签 ios swift uiimage cgrect uigraphicscontext

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

image

@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
}

最佳答案

首先,您必须从原始图像中裁剪出一个正方形。假设图像是 landscape 模式图像。所以宽度>高度。因此生成的正方形图像大小为 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
        }

例子 enter image description here 这里是黄色背景的 ImageView ,其中内容模式设置为宽高比。所以 imageview 的背景颜色是可见的,因为纵横比不匹配。

我的输出 enter image description here

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

希望它能为您服务。

关于ios - 以方形格式保存照片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54283847/

相关文章:

ios - 无法使用 UIImagePicker 平移从相机拍摄的图像

ios - AFNetworking http 基本身份验证给出错误 "Your API key was not formatted properly"

ios - Apple 开发者注册过期后,您还可以从 iOS 访问您的应用程序吗?

ios - 从 Alamofire 下载请求访问文件

iphone - UIImageView:内容模式宽高比扭曲图像

swift - 从示例缓冲区获取 UIImage 时 Google Vision 崩溃

ios - 使用预定义数据填充核心数据?

objective-c - 通过uitableview索引路径行在同一个uiviewcontroller中显示不同的内容 View

ios - 在 Swift 中运行 Firebase 时出错

SWIFT 在 SQL 中使用 INT