【问题标题】:Make UIImage a Circle without Losing quality? (swift)让 UIImage 变成圆形而不损失质量? (迅速)
【发布时间】:2015-11-18 05:13:51
【问题描述】:

应用roundImage后图像变得模糊: Making a UIImage to a circle form

extension UIImage
{
    func roundImage() -> UIImage
    {
        let newImage = self.copy() as! UIImage
        let cornerRadius = self.size.height/2
        UIGraphicsBeginImageContextWithOptions(self.size, false, 1.0)
        let bounds = CGRect(origin: CGPointZero, size: self.size)
        UIBezierPath(roundedRect: bounds, cornerRadius: cornerRadius).addClip()
        newImage.drawInRect(bounds)
        let finalImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return finalImage
    }
}

【问题讨论】:

    标签: ios swift uiimage


    【解决方案1】:

    您为什么使用bezierpath?只需将cornerradius 设置为uiimageview

    如果您的图像大于imageview,则必须将image 调整为imageview 大小,然后为uiimageview 设置cornerradius

    它会起作用的。为我工作

    替换下面一行

    UIGraphicsBeginImageContextWithOptions(self.size, false, 1.0)
    

    UIGraphicsBeginImageContextWithOptions(self.size, view.opaque , 0.0)
    

    【讨论】:

    • 我不能使用 UIImageView 来实现。
    • 这是我正在设置的 MKAnnotationView 的图像
    • 更新了答案。看看并尝试
    【解决方案2】:

    试试这个

    让 image = UIImageView(frame: CGRectMake(0, 0, 100, 100))

    【讨论】:

      【解决方案3】:

      我建议你可以使用 AlamofireImage (https://github.com/Alamofire/AlamofireImage)

      制作圆形图片或圆形图片非常容易,而且不会损失质量。

      就像这样:

      let image = UIImage(named: "unicorn")!
      let radius: CGFloat = 20.0
      
      let roundedImage = image.af_imageWithRoundedCornerRadius(radius)
      let circularImage = image.af_imageRoundedIntoCircle()
      

      瞧!

      【讨论】:

      • 太棒了! `AlamofireImage' 是最简单的方法!谢谢@Joey Wong
      【解决方案4】:

      您的问题是您使用的是 1 级,这是最低的“质量”。 将比例设置为 0 将使用设备比例,它只是按原样使用图像。

      附注:类中返回该类的新实例的函数可以实现为类函数。这使得函数的作用非常清楚。它不操纵现有图像。它返回一个新的。

      由于您在谈论圆圈,我还更正了您的代码,因此它现在可以为任何图像制作一个圆圈并对其进行裁剪。您可能希望将其居中。

      extension UIImage {
      
          class func roundImage(image : UIImage) -> UIImage? {
      
              // copy
              guard let newImage = image.copy() as? UIImage else {
                  return nil
              }
              // start context
              UIGraphicsBeginImageContextWithOptions(newImage.size, false, 0.0)
      
              // bounds
              let cornerRadius = newImage.size.height / 2
              let minDim = min(newImage.size.height, newImage.size.width)
              let bounds = CGRect(origin: CGPointZero, size: CGSize(width: minDim, height: minDim))
              UIBezierPath(roundedRect: bounds, cornerRadius: cornerRadius).addClip()
      
              // new image
              newImage.drawInRect(bounds)
              let finalImage = UIGraphicsGetImageFromCurrentImageContext()
              UIGraphicsEndImageContext()
      
              // crop
              let maybeCrop = UIImage.crop(finalImage, cropRect: bounds)
      
              return maybeCrop
      
          }
      
          class func crop(image: UIImage, cropRect : CGRect) -> UIImage? {
      
              guard let imgRef = CGImageCreateWithImageInRect(image.CGImage, cropRect) else {
                  return nil
              }
              return UIImage(CGImage: imgRef)
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-02-13
        • 1970-01-01
        • 2012-09-03
        • 2012-07-13
        • 2014-03-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多