为什么在设置半径和大小时我的图像质量会下降?
Why is my image quality decreasing when setting the radius and size?
我在 UIImage 扩展中使用以下方法来使用 URL 中的图像并将其显示为 UIBarButtonItem。但是,当我使用以下方法调整角半径和大小时,图像质量明显变差。需要改变什么以防止质量下降?
extension UIImage {
public func withRoundedCornersAndSetSize(radius: CGFloat? = nil, size: CGSize) -> UIImage? {
let maxRadius = min(size.width, size.height) / 2
let cornerRadius: CGFloat
if let radius = radius, radius > 0 && radius <= maxRadius {
cornerRadius = radius
} else {
cornerRadius = maxRadius
}
UIGraphicsBeginImageContextWithOptions(size, false, scale)
let rect = CGRect(origin: .zero, size: size)
UIBezierPath(roundedRect: rect, cornerRadius: cornerRadius).addClip()
draw(in: rect)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
}
我发现我可以使用以下代码中的 resize()
方法调整 UIImage 的大小而不会降低质量。然后,我只是在我最初询问的 withRoundedCornersAndSetSize()
方法中使用了它。
extension UIImage {
func withRoundedCornersAndSetSize(radius: CGFloat? = nil, targetSize: CGSize) -> UIImage? {
let resizedImage = self.resize(targetSize: targetSize)
let maxRadius = min(resizedImage.size.width, resizedImage.size.height) / 2
let cornerRadius: CGFloat
if let radius = radius, radius > 0 && radius <= maxRadius {
cornerRadius = radius
} else {
cornerRadius = maxRadius
}
UIGraphicsBeginImageContextWithOptions(resizedImage.size, false, resizedImage.scale)
let rect = CGRect(origin: .zero, size: resizedImage.size)
UIBezierPath(roundedRect: rect, cornerRadius: cornerRadius).addClip()
draw(in: rect)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image!
}
func resize(targetSize: CGSize) -> UIImage {
return UIGraphicsImageRenderer(size:targetSize).image { _ in
self.draw(in: CGRect(origin: .zero, size: targetSize))
}
}
}
我在 UIImage 扩展中使用以下方法来使用 URL 中的图像并将其显示为 UIBarButtonItem。但是,当我使用以下方法调整角半径和大小时,图像质量明显变差。需要改变什么以防止质量下降?
extension UIImage {
public func withRoundedCornersAndSetSize(radius: CGFloat? = nil, size: CGSize) -> UIImage? {
let maxRadius = min(size.width, size.height) / 2
let cornerRadius: CGFloat
if let radius = radius, radius > 0 && radius <= maxRadius {
cornerRadius = radius
} else {
cornerRadius = maxRadius
}
UIGraphicsBeginImageContextWithOptions(size, false, scale)
let rect = CGRect(origin: .zero, size: size)
UIBezierPath(roundedRect: rect, cornerRadius: cornerRadius).addClip()
draw(in: rect)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
}
我发现我可以使用以下代码中的 resize()
方法调整 UIImage 的大小而不会降低质量。然后,我只是在我最初询问的 withRoundedCornersAndSetSize()
方法中使用了它。
extension UIImage {
func withRoundedCornersAndSetSize(radius: CGFloat? = nil, targetSize: CGSize) -> UIImage? {
let resizedImage = self.resize(targetSize: targetSize)
let maxRadius = min(resizedImage.size.width, resizedImage.size.height) / 2
let cornerRadius: CGFloat
if let radius = radius, radius > 0 && radius <= maxRadius {
cornerRadius = radius
} else {
cornerRadius = maxRadius
}
UIGraphicsBeginImageContextWithOptions(resizedImage.size, false, resizedImage.scale)
let rect = CGRect(origin: .zero, size: resizedImage.size)
UIBezierPath(roundedRect: rect, cornerRadius: cornerRadius).addClip()
draw(in: rect)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image!
}
func resize(targetSize: CGSize) -> UIImage {
return UIGraphicsImageRenderer(size:targetSize).image { _ in
self.draw(in: CGRect(origin: .zero, size: targetSize))
}
}
}