将UIImage调整为200x200pt/px大小

90

我一直在努力调整图像大小。基本上,我遇到了以下问题: 如何缩小UIImage并使其同时变得清晰 / 锐利而不是模糊的?

这似乎是一个合适的解决方案,但不知何故它无法正常工作。

我的应用程序使用来自相机胶卷的照片。 这些照片应该调整为约200x200的尺寸,其中宽度很重要,而高度不重要。

不幸的是,由于我的愤怒导致无法工作的解决方案,我没有示例代码,抱歉。


什么东西出了问题?你期望得到什么结果,实际得到了什么结果? - ifau
我希望得到一张宽度为200像素的图片,并且按照比例计算高度。但是我仍然得到了原始图像,例如1920x...任何尺寸。 - swift_dan
13个回答

2

如果你在项目中使用Kingfisher库来加载图片,并且想要调整其大小,以下是方法:

  • Xcode 8
  • Swift 3x
  • 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的答案。


    1
    这是对@Christoph R在Swift 3.0中回答的延续。这段代码适用于Swift 5.0.1。
    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))
    

    0

    通过减小图片大小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
            }
    
        }
    

    网页内容由stack overflow 提供, 点击上面的
    可以查看英文原文,
    原文链接