iOS Swift UIImage 调整大小影响性能的问题;调整大小时出现意外尺寸[重复]

Posted

技术标签:

【中文标题】iOS Swift UIImage 调整大小影响性能的问题;调整大小时出现意外尺寸[重复]【英文标题】:iOS Swift UIImage resizing issue affecting performance; unexpected dimensions upon resizing [duplicate] 【发布时间】:2019-11-16 01:18:20 【问题描述】:

我在 tableView(非常生涩)和几个视图控制器(加载缓慢)中遇到了性能问题。为了加快速度并使事情顺利进行,我尝试在初始加载时将传入图像的大小调整为它们各自 imageViews 的确切大小,而不是每次从 Core Data 加载时都在运行中。调整大小的图像将作为单独的属性存储在其父实体上。所有图片都是正方形的,即 1:1 的纵横比。

这是调整大小的代码:

extension UIImage 

    func resize(targetSize: CGSize) -> UIImage 

        print("Inside resize function")
        print("targetSize is \(targetSize)")

        return UIGraphicsImageRenderer(size:targetSize).image  _ in
            self.draw(in: CGRect(origin: .zero, size: targetSize))
        
    

这是调用调整大小函数并返回的函数:

    func difAndAssignPics() 

        guard let appDelegate =
            UIApplication.shared.delegate as? AppDelegate else 
                return
        
// This is a copy of the original image, from which the smaller ones will be created

        print("bigToLittlePic size is \(bigToLittlePic?.size as Any)")

// These are the desired sizes of the three images I'm trying to create to facilitate performance

        let littlePicSize = CGSize(width: 110, height: 110)
        let mediumPicSize = CGSize(width: 160, height: 160)
        let displayPicSize = CGSize(width: 340, height: 340)

// This is the call to the resize function for each target size

        littlePic = bigToLittlePic?.resize(targetSize: littlePicSize)
        mediumPic = bigToLittlePic?.resize(targetSize: mediumPicSize)
        displayPic = bigToLittlePic?.resize(targetSize: displayPicSize)

// This code differentiates between front view and rear view of the item and prints out the size of each

        switch switchTag 
        case 1:
            newCoin.obversePic = (displayPic)!.pngData()! as NSData
            newCoin.obversePicThumb = (littlePic)!.pngData()! as NSData
            newCoin.obversePicMedium = (mediumPic)!.pngData()! as NSData

            selectIndicatorLabel.text = "Now select Reverse photo"
            appDelegate.saveContext()

            print("Inside switch case 1, difAndAssignPics")

            if let imageData = newCoin.obversePicThumb
                let image = UIImage(data: imageData as Data)
                print("obversePicThumb size is \(image?.size as Any)")
            
            if let imageData = newCoin.obversePicMedium
                let image = UIImage(data: imageData as Data)
                print("obversePicMedium size is \(image?.size as Any)")
            
            if let imageData = newCoin.obversePic
                let image = UIImage(data: imageData as Data)
                print("obversePicBig size is \(image?.size as Any)")
            


        case 2:
            newCoin.reversePic = (displayPic)!.pngData()! as NSData
            newCoin.reversePicThumb = (littlePic)!.pngData()! as NSData
            newCoin.reversePicMedium = (mediumPic)!.pngData()! as NSData

            selectIndicatorLabel.text = "Now select Obverse photo"
            appDelegate.saveContext()

            print("Inside switch case 2, difAndAssignPics")

            if let imageData = newCoin.reversePicThumb
                let image = UIImage(data: imageData as Data)
                print("reversePicThumb size is \(image?.size as Any)")
            
            if let imageData = newCoin.reversePicMedium
                let image = UIImage(data: imageData as Data)
                print("reversePicMedium size is \(image?.size as Any)")
            
            if let imageData = newCoin.reversePic
                let image = UIImage(data: imageData as Data)
                print("reversePicBig size is \(image?.size as Any)")
            
        default: break
        
        reactivateDataButtons()
    

以下是每张新图片介绍时控制台中打印的内容:

bigToLittlePic size is Optional((2320.0, 2320.0))

Inside resize function
targetSize is (110.0, 110.0)
Inside resize function
targetSize is (160.0, 160.0)
Inside resize function
targetSize is (340.0, 340.0)

好的,到目前为止一切顺利。但是,当图像返回到difAndAssignPics 时,这是打印输出:

reversePicThumb size is Optional((330.0, 330.0))
reversePicMedium size is Optional((480.0, 480.0))
reversePicBig size is Optional((1020.0, 1020.0))

为简洁起见,我只包含了反向图像的打印输出。正面给出相同的数字。

如您所见,不知何故,每个调整大小的图像的大小都膨胀了 3 倍。图片加载,质量很高,但性能仍然明显欠佳。我不知道如何核对这些数字。

有人有什么建议吗?

非常感谢!

编辑

我打印出在我的自定义单元格中被压缩到 110 x 110 imageViews 中的图像的大小。这些数字证实了调整大小功能中的某些东西被弄坏了。这是cellForRow中的数字:

obversePicThumb.size == Optional((330.0, 330.0))

reversePicThumb.size == Optional((330.0, 330.0))

编辑#2

post 回答了我的问题。非常感谢那些花时间看的人!

【问题讨论】:

这是因为视网膜显示屏。三个像素映射到设备上的一个点。见***.com/questions/38045980/… @Bill -- 是的,先生,我这样做了,它回答了我的问题!非常感谢! 【参考方案1】:

我不知道这是否会有所不同,但这是我的扩展来完成此操作:

extension UIImage 
    public func resizeToBoundingSquare(_ boundingSquareSideLength : CGFloat) -> UIImage 
        let imgScale = self.size.width > self.size.height ? boundingSquareSideLength / self.size.width : boundingSquareSideLength / self.size.height
        let newWidth = self.size.width * imgScale
        let newHeight = self.size.height * imgScale
        let newSize = CGSize(width: newWidth, height: newHeight)
        UIGraphicsBeginImageContext(newSize)
        self.draw(in: CGRect(x: 0, y: 0, width: newWidth, height: newHeight))
        let resizedImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext();
        return resizedImage!
    

我的用法不是UITableView,也不是CoreData。 (我建议对类似的事情进行异步处理。)但是如果您的问题是调整大小,那么这应该具有可接受的性能。

【讨论】:

@dfd--加载速度大大提升。不幸的是,图像的质量不是很好,我需要最好的分辨率来缩放细节图像。但是,非常感谢您的算法!我也许可以修改它以获得更清晰的图像。如果我这样做了,我会告诉你的。 有关缩放问题的解决方案,请参阅上面的链接。它解决了我的问题,可能对你也有帮助......

以上是关于iOS Swift UIImage 调整大小影响性能的问题;调整大小时出现意外尺寸[重复]的主要内容,如果未能解决你的问题,请参考以下文章

UIImage 未根据 UIImageView 大小宽度和高度调整大小

iOS - 调整图像大小导致内存泄漏

逐帧调整动画 UIImage 的大小非常慢

如何调整正在打印的图像的大小?

在裁剪之前调整 UIImage 或 CIImage 的大小会拉伸图像吗?

swift UIImage使用UIImageView调整方面填充和裁剪