【发布时间】: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 回答了我的问题。非常感谢那些花时间看的人!
【问题讨论】:
-
这是因为视网膜显示屏。三个像素映射到设备上的一个点。见stackoverflow.com/questions/38045980/…
-
@Bill -- 是的,先生,我这样做了,它回答了我的问题!非常感谢!
标签: ios swift uiimageview uiimage image-resizing