我一直在努力调整图像大小。基本上,我遇到了以下问题: 如何缩小UIImage并使其同时变得清晰 / 锐利而不是模糊的?
这似乎是一个合适的解决方案,但不知何故它无法正常工作。
我的应用程序使用来自相机胶卷的照片。 这些照片应该调整为约200x200的尺寸,其中宽度很重要,而高度不重要。
不幸的是,由于我的愤怒导致无法工作的解决方案,我没有示例代码,抱歉。
我一直在努力调整图像大小。基本上,我遇到了以下问题: 如何缩小UIImage并使其同时变得清晰 / 锐利而不是模糊的?
这似乎是一个合适的解决方案,但不知何故它无法正常工作。
我的应用程序使用来自相机胶卷的照片。 这些照片应该调整为约200x200的尺寸,其中宽度很重要,而高度不重要。
不幸的是,由于我的愤怒导致无法工作的解决方案,我没有示例代码,抱歉。
如果你在项目中使用Kingfisher库来加载图片,并且想要调整其大小,以下是方法:
let imageUrl = URL(string: "your image url")
//Size refer to the size which you want to resize your original image
let size = CGSize(width: 60, height: 60)
let processImage = ResizingImageProcessor(targetSize: size, contentMode: .aspectFit)
cell.courseTitleImage.kf.setImage(with: imageUrl! , placeholder: UIImage(named: "placeholder"), options: [.transition(ImageTransition.fade(1)), .processor(processImage)], progressBlock: nil, completionHandler: nil)
或者
调整本地图片大小:- 您可以参考@Christoph R的答案。
static func resizeImage(image: UIImage, newWidth: CGFloat) -> UIImage {
let scale = newWidth / image.size.width
let newHeight = image.size.height * scale
UIGraphicsBeginImageContext(CGSize(width: newWidth, height: newHeight))
image.draw(in: CGRect(x: 0, y: 0, width: newWidth, height: newHeight))
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage!
}
TaskUtilties.resizeImage(image: rawImage!, newWidth: CGFloat(50))
通过减小图片大小1024,您可以根据服务器容量随时进行转换
func resizeImage(image: UIImage) -> UIImage {
if image.size.height >= 1024 && image.size.width >= 1024 {
UIGraphicsBeginImageContext(CGSize(width:1024, height:1024))
image.draw(in: CGRect(x:0, y:0, width:1024, height:1024))
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage!
}
else if image.size.height >= 1024 && image.size.width < 1024
{
UIGraphicsBeginImageContext(CGSize(width:image.size.width, height:1024))
image.draw(in: CGRect(x:0, y:0, width:image.size.width, height:1024))
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage!
}
else if image.size.width >= 1024 && image.size.height < 1024
{
UIGraphicsBeginImageContext(CGSize(width:1024, height:image.size.height))
image.draw(in: CGRect(x:0, y:0, width:1024, height:image.size.height))
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage!
}
else
{
return image
}
}