gpt4 book ai didi

ios - 在应用程序中加载多个保存的图像会减慢速度

转载 作者:搜寻专家 更新时间:2023-11-01 07:34:25 25 4
gpt4 key购买 nike

我正在开发一个应用程序,用户可以在其中保存 13 个屏幕截图并将它们作为缩略图或全屏图像显示在单个 View 中。

let fileName:String = self.stickerUsed + ".png"
var arrayPaths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as NSString
var pngFileName = arrayPaths.stringByAppendingPathComponent(fileName)
UIImagePNGRepresentation(screenshot).writeToFile(pngFileName, atomically:true)
NSUserDefaults.standardUserDefaults().setObject(fileName, forKey: self.stickerUsed)
NSUserDefaults.standardUserDefaults().synchronize()

上面是我保存图像的方式,下面是我检索图像的方式。这是第一个屏幕截图的代码:

var defaultName:String = "Sticker1.png"
let path = NSSearchPathForDirectoriesInDomains(
.DocumentDirectory, .UserDomainMask, true)[0] as NSString
let fileName = NSUserDefaults.standardUserDefaults()
.stringForKey("Sticker1") ?? defaultName
let imagePath = path.stringByAppendingPathComponent(fileName)
let image = UIImage(contentsOfFile: imagePath )

问题是随着屏幕截图数量的增加,在单个 View 中显示它们变得非常慢。最终,应用程序在显示“收到内存警告”后崩溃。我是 swift 和应用程序开发的新手。在不减慢或崩溃的情况下以缩略图显示所有这些图像并以全分辨率保存图像的正确方法是什么。

最佳答案

以下代码的问题在于您将获得全分辨率的所有图像:

let image = UIImage(contentsOfFile: imagePath )

加载到内存后最好立即缩小并制作缩略图:

// This line is still the same
let image = UIImage(contentsOfFile: imagePath )

// Scale the image down:
let newSize = ... // Defined the new size here
// should probably be a good idea to match
// the size of the UIImageView
UIGraphicsBeginImageContext(newSize)
image.drawInRect(CGRect(origin: CGPointZero, size: newSize))
image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()

查看更多:link .

关于ios - 在应用程序中加载多个保存的图像会减慢速度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30818918/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com