在应用程序中加载多个保存的图像会减慢速度
Loading multiple saved images in app slows it down
我正在开发一个应用程序,用户可以在其中保存 13 个屏幕截图并将它们作为缩略图或全屏图像显示在单个视图中。
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 )
问题是随着屏幕截图数量的增加,在单个视图中显示它们变得非常慢。最终应用程序在显示 "Received memory warning" 后崩溃。我是 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。
我正在开发一个应用程序,用户可以在其中保存 13 个屏幕截图并将它们作为缩略图或全屏图像显示在单个视图中。
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 )
问题是随着屏幕截图数量的增加,在单个视图中显示它们变得非常慢。最终应用程序在显示 "Received memory warning" 后崩溃。我是 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。