iOS 8 应用程序出现内存警告 - 但使用率很低
Memory warning on iOS 8 App - but usage is low
我希望有人能帮助我,我已经搜索了 Whosebug 和 Google 但我找不到正确的解决方案。
我有一个非常简单的应用程序,它拍摄照片(通过 UIImagePickerController 使用标准 iOS 相机)然后我以非常低的分辨率将它保存到文件系统 - let thumbNailData = UIImageJPEGRepresentation(image, 0.02)
之后我将图像显示在使用 Core Data 的集合视图——我只在 Core Data 中保存文件名,而不是图像,图像只在文件系统中。
因此,但是,当我 运行 该应用程序时,它显示内存使用量不超过 15 MB,进程大约为 1-2%。一切 运行 都很好,但在添加 6-7 张照片后,我收到一些奇怪的错误,例如内存警告、与我的 iPhone 失去连接以及此:
Communications error: <OS_xpc_error: <error: 0x198adfa80> { count = 1, contents =
"XPCErrorDescription" => <string: 0x198adfe78> { length = 22, contents = "Connection interrupted"
所以,我真的卡住了,我以为我把它做得很轻,然后我得到了这些错误....
我已经向 App Store 提交了一个笔记应用程序,它的功能比这个高得多,但是这个 运行 非常稳定...
有什么想法吗?
这是我的一些代码:
// 这里我将图片名称加载到一个数组中,在collection view中显示
func loadColl () {
let appDelegate = (UIApplication.sharedApplication().delegate as AppDelegate)
let context:NSManagedObjectContext = appDelegate.managedObjectContext!
let fetchRequest:NSFetchRequest = NSFetchRequest(entityName: "PictureNote")
var error:NSError?
var result = context.executeFetchRequest(fetchRequest, error: &error) as [PictureNote]
for res in result {
println(res.latitude)
println(res.longitude)
println(res.longDate)
println(res.month)
println(res.year)
println(res.text)
pictures.append(res.thumbnail)
}
}
// 这是在集合视图中显示的代码
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let myImageCell:ImageCell = myCollectionView.dequeueReusableCellWithReuseIdentifier("imageCell", forIndexPath: indexPath) as ImageCell
myImageCell.imageCell.image = self.loadImageFromPath(fileInDocumentsDirectory(self.pictures[indexPath.row]))
return myImageCell
}
// 这是从磁盘加载图片的代码
func loadImageFromPath(path: String) -> UIImage {
let image = UIImage(contentsOfFile: path)
if image == nil {
self.newAlert("Error loading your image, try again", title: "Notice")
}
return image!
}
// 这是我的保存码
func saveNewEntry () {
var unique = NSUUID().UUIDString
var imageTitle = "Picture\(unique).jpg"
var image = self.pickedImageView.image
var path = fileInDocumentsDirectory(imageTitle)
var thumbImageTitle = "ThumbPicture\(unique).jpg"
var thumbPath = fileInDocumentsDirectory(thumbImageTitle)
if self.saveImage(image!, path: path) == true && self.saveThumbImage(image!, thumbPath: thumbPath) == true {
// Create the saving context
let context = (UIApplication.sharedApplication().delegate as AppDelegate).managedObjectContext!
let entityOne = NSEntityDescription.entityForName("PictureNote", inManagedObjectContext: context)
let thisTask = PictureNote(entity: entityOne!, insertIntoManagedObjectContext: context)
// Get all the values here
self.getMyLocation()
var theDate = NSDate()
thisTask.month = Date.toString(date: theDate, format: "MM")
thisTask.year = Date.toString(date: theDate, format: "yyyy")
thisTask.longDate = theDate
thisTask.longitude = getMyLocation()[1]
thisTask.latitude = getMyLocation()[0]
thisTask.text = self.noteTextView.text
thisTask.imageURL = imageTitle
thisTask.thumbnail = thumbImageTitle
thisTask.id = unique
// Saving to CoreData
if context.save(nil) {
self.newAlert("Saving your note was successful!", title: "Notice")
self.noteTextView.text = ""
} else {
self.newAlert("Error saving your note, try again", title: "Notice")
}
} else {
self.newAlert("Error saving your image, try again", title: "Notice")
}
self.pickedImageView.image = UIImage(named: "P1000342.jpg")
}
非常感谢您的每一个建议....如果您需要更多代码,请告诉我...
我注意到您在 UIImageJPEGRepresentation
中使用了显着降低的品质因数。如果这是尝试减少所涉及的内存,那么所做的只是减少结果 NSData
写入持久存储的大小,但是将该图像加载到图像视图中仍然需要大约 ( 4 × width × height) 字节(注意,这是图像的尺寸,而不是图像视图)。因此,来自 iPhone 的 3264 × 2448 图像每张图像占用 30mb,无论 UIImageJPEGRepresentation
.
使用的质量因子如何
通常我会确保我的 collection/table 视图使用缩略图表示(ALAsset
的 thumbnail
属性 或 我自己)。如果我在任何地方缓存这个缩略图(比如你的问题建议的持久存储),我就可以使用缩略图的高质量表示(我使用 PNG 是因为它在压缩时是无损的,但是质量因子为 0.7-0.9 的 JPEG 是一个也相当忠实的版本)。
我希望有人能帮助我,我已经搜索了 Whosebug 和 Google 但我找不到正确的解决方案。
我有一个非常简单的应用程序,它拍摄照片(通过 UIImagePickerController 使用标准 iOS 相机)然后我以非常低的分辨率将它保存到文件系统 - let thumbNailData = UIImageJPEGRepresentation(image, 0.02)
之后我将图像显示在使用 Core Data 的集合视图——我只在 Core Data 中保存文件名,而不是图像,图像只在文件系统中。
因此,但是,当我 运行 该应用程序时,它显示内存使用量不超过 15 MB,进程大约为 1-2%。一切 运行 都很好,但在添加 6-7 张照片后,我收到一些奇怪的错误,例如内存警告、与我的 iPhone 失去连接以及此:
Communications error: <OS_xpc_error: <error: 0x198adfa80> { count = 1, contents =
"XPCErrorDescription" => <string: 0x198adfe78> { length = 22, contents = "Connection interrupted"
所以,我真的卡住了,我以为我把它做得很轻,然后我得到了这些错误.... 我已经向 App Store 提交了一个笔记应用程序,它的功能比这个高得多,但是这个 运行 非常稳定...
有什么想法吗?
这是我的一些代码: // 这里我将图片名称加载到一个数组中,在collection view中显示
func loadColl () {
let appDelegate = (UIApplication.sharedApplication().delegate as AppDelegate)
let context:NSManagedObjectContext = appDelegate.managedObjectContext!
let fetchRequest:NSFetchRequest = NSFetchRequest(entityName: "PictureNote")
var error:NSError?
var result = context.executeFetchRequest(fetchRequest, error: &error) as [PictureNote]
for res in result {
println(res.latitude)
println(res.longitude)
println(res.longDate)
println(res.month)
println(res.year)
println(res.text)
pictures.append(res.thumbnail)
}
}
// 这是在集合视图中显示的代码
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let myImageCell:ImageCell = myCollectionView.dequeueReusableCellWithReuseIdentifier("imageCell", forIndexPath: indexPath) as ImageCell
myImageCell.imageCell.image = self.loadImageFromPath(fileInDocumentsDirectory(self.pictures[indexPath.row]))
return myImageCell
}
// 这是从磁盘加载图片的代码
func loadImageFromPath(path: String) -> UIImage {
let image = UIImage(contentsOfFile: path)
if image == nil {
self.newAlert("Error loading your image, try again", title: "Notice")
}
return image!
}
// 这是我的保存码
func saveNewEntry () {
var unique = NSUUID().UUIDString
var imageTitle = "Picture\(unique).jpg"
var image = self.pickedImageView.image
var path = fileInDocumentsDirectory(imageTitle)
var thumbImageTitle = "ThumbPicture\(unique).jpg"
var thumbPath = fileInDocumentsDirectory(thumbImageTitle)
if self.saveImage(image!, path: path) == true && self.saveThumbImage(image!, thumbPath: thumbPath) == true {
// Create the saving context
let context = (UIApplication.sharedApplication().delegate as AppDelegate).managedObjectContext!
let entityOne = NSEntityDescription.entityForName("PictureNote", inManagedObjectContext: context)
let thisTask = PictureNote(entity: entityOne!, insertIntoManagedObjectContext: context)
// Get all the values here
self.getMyLocation()
var theDate = NSDate()
thisTask.month = Date.toString(date: theDate, format: "MM")
thisTask.year = Date.toString(date: theDate, format: "yyyy")
thisTask.longDate = theDate
thisTask.longitude = getMyLocation()[1]
thisTask.latitude = getMyLocation()[0]
thisTask.text = self.noteTextView.text
thisTask.imageURL = imageTitle
thisTask.thumbnail = thumbImageTitle
thisTask.id = unique
// Saving to CoreData
if context.save(nil) {
self.newAlert("Saving your note was successful!", title: "Notice")
self.noteTextView.text = ""
} else {
self.newAlert("Error saving your note, try again", title: "Notice")
}
} else {
self.newAlert("Error saving your image, try again", title: "Notice")
}
self.pickedImageView.image = UIImage(named: "P1000342.jpg")
}
非常感谢您的每一个建议....如果您需要更多代码,请告诉我...
我注意到您在 UIImageJPEGRepresentation
中使用了显着降低的品质因数。如果这是尝试减少所涉及的内存,那么所做的只是减少结果 NSData
写入持久存储的大小,但是将该图像加载到图像视图中仍然需要大约 ( 4 × width × height) 字节(注意,这是图像的尺寸,而不是图像视图)。因此,来自 iPhone 的 3264 × 2448 图像每张图像占用 30mb,无论 UIImageJPEGRepresentation
.
通常我会确保我的 collection/table 视图使用缩略图表示(ALAsset
的 thumbnail
属性 或