【问题标题】:how to make bordered image in iOS?如何在iOS中制作带边框的图像?
【发布时间】:2017-02-06 08:19:42
【问题描述】:

我在Saving UIImage with rounded corners and border with Swift之后试过了。

但图像上的边框重叠。

我需要干净的边框图像。

我该怎么做?

【问题讨论】:

    标签: ios swift uiimage


    【解决方案1】:

    试试 imageView.contentMode = .scaleAspectFit 或者 imageView.contentMode = .center

    您也可以通过情节提要来做到这一点。

    【讨论】:

      【解决方案2】:

      您最好的方法是确保包含图片的 imageView 与您所包围的图片完全成比例,因此任何边缘都没有间隙。那么imageView的layer.borderColor和layer.borderWidth属性就很好用了。

      这是一个示例代码:

          import UIKit
      
      class ViewController: UIViewController {
          var pippa: UIImageView!
          override func viewDidLoad() {
              super.viewDidLoad()
              let pippaPicture = UIImage(named: "user")
              let s = pippaPicture?.size
              let width = view.frame.width
              let height = (width/(s?.width)!) * (s?.height)!
              pippa = UIImageView(frame:CGRect(x:0, y:0, width:width, height:height))
              pippa.image = pippaPicture
              print(s!, width, height)
              pippa.layer.borderWidth = 5
              pippa.layer.borderColor = UIColor.red.cgColor
              view.addSubview(pippa)
          }
      }
      

      【讨论】:

      • 谢谢。但我需要一种方法来保存干净的边框图像或临时获取。我正在从 Document 文件夹或 imageview 加载图像
      【解决方案3】:

      这是我最新的代码。

      func addEdgeAction(image: UIImage) -> UIImage{
              let imageView: UIImageView = UIImageView(image: image)
      
              let bgview: UIView = UIView()
              bgview.frame = CGRect(x: 0, y: 0, width: imageView.frame.size.width + 200, height: imageView.frame.size.height + 200)
              var layer: CALayer = CALayer()
              layer = bgview.layer
              layer.masksToBounds = true
              layer.cornerRadius = CGFloat(8)
              layer.borderColor = UIColor.clear.cgColor
              layer.borderWidth = 100
      
              let imageView1: UIImageView = UIImageView(image: image)
              imageView1.frame = CGRect(x: 100, y: 100, width: imageView.frame.size.width, height: imageView.frame.size.height)
              bgview.addSubview(imageView1)
      
              UIGraphicsBeginImageContext(bgview.layer.bounds.size)
              layer.render(in: UIGraphicsGetCurrentContext()!)
              let roundedImage = UIGraphicsGetImageFromCurrentImageContext()
              UIGraphicsEndImageContext()
              //secImg.image = roundedImage
      
              return roundedImage!
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-10-24
        • 2015-07-24
        • 1970-01-01
        • 2016-06-07
        • 1970-01-01
        • 2021-07-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多