使用 pngdata 转换图像会导致应用程序冻结然后崩溃
Converting Image using pngdata causes app to freeze then crash
我正在使用以下代码将图像转换为数据以将它们存储在核心数据中,但是由于 运行 内存不足导致应用程序崩溃。该应用程序也被冻结,直到代码完成
DispatchQueue.main.async {
let fetchRequest: NSFetchRequest<Project> = Project.fetchRequest()
fetchRequest.sortDescriptors = [NSSortDescriptor(key: "name", ascending: true)]
do {
let results = try self.appData.moc.fetch(fetchRequest)
let fileManager = FileManager.default
for project in results {
NSLog("Project Name: \(String(describing: project.name))")
if let mainPic = project.mainPicture {
if let mainPicName = mainPic.pictureName {
let imagePath = (NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as NSString).appendingPathComponent(mainPicName)
if fileManager.fileExists(atPath: imagePath) {
if let image : UIImage = UIImage(contentsOfFile: imagePath) {
let imageData = image.pngData()
}
}
}
}
}
} catch {
print("Fetching Failed")
}
self.appData.saveContext()
NSLog("13 Ran")
}
如果我注释掉 let imageData = image.pngData() 行,那么我就不会遇到问题
注释掉该行后,应用程序加载后的内存使用量小于 100MB,如果不注释它会达到 1.15GB,然后再回到大约 150MB。
我认为这是内存问题。
获取了多少个项目?
您可以将代码更改为:
DispatchQueue.main.async {
let fetchRequest: NSFetchRequest<Project> = Project.fetchRequest()
fetchRequest.sortDescriptors = [NSSortDescriptor(key: "name", ascending: true)]
do {
let results = try self.appData.moc.fetch(fetchRequest)
let fileManager = FileManager.default
for project in results {
autoreleasepool {
NSLog("Project Name: \(String(describing: project.name))")
if let mainPic = project.mainPicture {
if let mainPicName = mainPic.pictureName {
let imagePath = (NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as NSString).appendingPathComponent(mainPicName)
if fileManager.fileExists(atPath: imagePath) {
if let image : UIImage = UIImage(contentsOfFile: imagePath) {
let imageData = image.pngData()
}
}
}
}
}
}
} catch {
print("Fetching Failed")
}
self.appData.saveContext()
NSLog("13 Ran")
}
我觉得应该没问题
我正在使用以下代码将图像转换为数据以将它们存储在核心数据中,但是由于 运行 内存不足导致应用程序崩溃。该应用程序也被冻结,直到代码完成
DispatchQueue.main.async {
let fetchRequest: NSFetchRequest<Project> = Project.fetchRequest()
fetchRequest.sortDescriptors = [NSSortDescriptor(key: "name", ascending: true)]
do {
let results = try self.appData.moc.fetch(fetchRequest)
let fileManager = FileManager.default
for project in results {
NSLog("Project Name: \(String(describing: project.name))")
if let mainPic = project.mainPicture {
if let mainPicName = mainPic.pictureName {
let imagePath = (NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as NSString).appendingPathComponent(mainPicName)
if fileManager.fileExists(atPath: imagePath) {
if let image : UIImage = UIImage(contentsOfFile: imagePath) {
let imageData = image.pngData()
}
}
}
}
}
} catch {
print("Fetching Failed")
}
self.appData.saveContext()
NSLog("13 Ran")
}
如果我注释掉 let imageData = image.pngData() 行,那么我就不会遇到问题
注释掉该行后,应用程序加载后的内存使用量小于 100MB,如果不注释它会达到 1.15GB,然后再回到大约 150MB。
我认为这是内存问题。
获取了多少个项目?
您可以将代码更改为:
DispatchQueue.main.async {
let fetchRequest: NSFetchRequest<Project> = Project.fetchRequest()
fetchRequest.sortDescriptors = [NSSortDescriptor(key: "name", ascending: true)]
do {
let results = try self.appData.moc.fetch(fetchRequest)
let fileManager = FileManager.default
for project in results {
autoreleasepool {
NSLog("Project Name: \(String(describing: project.name))")
if let mainPic = project.mainPicture {
if let mainPicName = mainPic.pictureName {
let imagePath = (NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as NSString).appendingPathComponent(mainPicName)
if fileManager.fileExists(atPath: imagePath) {
if let image : UIImage = UIImage(contentsOfFile: imagePath) {
let imageData = image.pngData()
}
}
}
}
}
}
} catch {
print("Fetching Failed")
}
self.appData.saveContext()
NSLog("13 Ran")
}
我觉得应该没问题