在不损失质量的情况下使 UIImage 成为一个圆圈? (swift)
Make UIImage a Circle without Losing quality? (swift)
应用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
}
}
试试这个
让图片 = UIImageView(frame: CGRectMake(0, 0, 100, 100))
为什么要使用 bezierpath
?只需为 uiimageview
.
设置 cornerradius
如果您的图像大于 imageview
,那么您必须将 image
的大小调整为 imageview
大小,然后为 uiimageview
设置 cornerradius
].
它会起作用的。适合我
替换下面一行
UIGraphicsBeginImageContextWithOptions(self.size, false, 1.0)
和
UIGraphicsBeginImageContextWithOptions(self.size, view.opaque , 0.0)
您的问题是您使用的是等级 1,这是最低的 "quality"。
将比例设置为 0 将使用设备比例,即按原样使用图像。
旁注:class 中的函数 return 可以将 class 的新实例实现为 class 函数。这使得该函数的作用非常清楚。它不会操纵现有图像。它 return 是新的。
既然你在谈论圆圈,我也更正了你的代码,现在它可以将任何图像做成一个圆圈并裁剪它。您可能希望将其居中。
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)
}
}
我推荐你可以使用 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()
瞧!
应用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
}
}
试试这个
让图片 = UIImageView(frame: CGRectMake(0, 0, 100, 100))
为什么要使用 bezierpath
?只需为 uiimageview
.
cornerradius
如果您的图像大于 imageview
,那么您必须将 image
的大小调整为 imageview
大小,然后为 uiimageview
设置 cornerradius
].
它会起作用的。适合我
替换下面一行
UIGraphicsBeginImageContextWithOptions(self.size, false, 1.0)
和
UIGraphicsBeginImageContextWithOptions(self.size, view.opaque , 0.0)
您的问题是您使用的是等级 1,这是最低的 "quality"。 将比例设置为 0 将使用设备比例,即按原样使用图像。
旁注:class 中的函数 return 可以将 class 的新实例实现为 class 函数。这使得该函数的作用非常清楚。它不会操纵现有图像。它 return 是新的。
既然你在谈论圆圈,我也更正了你的代码,现在它可以将任何图像做成一个圆圈并裁剪它。您可能希望将其居中。
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)
}
}
我推荐你可以使用 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()
瞧!