【问题标题】:Memory warning on iOS 8 App - but usage is lowiOS 8 应用程序上的内存警告 - 但使用率很低
【发布时间】:2015-03-01 14:19:40
【问题描述】:

我希望有人可以帮助我,我已经搜索过 Stackoverflow 和 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 提交了一个笔记应用程序,它的功能比这个要高得多,但是这个运行得非常稳定......

有什么想法吗?

这是我的一些代码: // 这里我将图片名称加载到一个数组中以显示在集合视图中

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")
    }

我真的很感谢每一个建议....如果您需要更多代码,请告诉我...

【问题讨论】:

  • 这不是内存警告,是Apple的框架导致了这个问题,这个问题之前确实发生过,也许你可以尝试更新你的Xcode和SDK。
  • 搜索“XPCErrorDescription”以获取更多信息。
  • 我知道,但我也收到了一个我不明白的内存警告,它在文本中......
  • 嗯,我在 Xcode 6.1.3 和 Xcode 6.2.5 上运行它......都是同一个问题......
  • 还要提防UIImage(named:)。您在方法结束时出现过一次,我不知道这是否真的是您经常使用的单个硬编码图像(在这种情况下,UIImage(named:) 很好)或者它是否表明使用该初始化程序其他地方也一样)。

标签: ios swift uicollectionview didreceivememorywarning


【解决方案1】:

我注意到您在使用UIImageJPEGRepresentation 的同时使用了显着降低的品质因数。如果这是尝试减少所涉及的内存,那么所做的只是减少生成的NSData 写入持久存储的大小,但是将该图像加载到图像视图中仍然需要大约 (4 × width × height) 字节(注意,这是图像的尺寸,而不是图像视图)。因此,来自 iPhone 的 3264 × 2448 图像每张图像占用 30mb,无论UIImageJPEGRepresentation 使用的质量因子如何。

通常我会确保我的集合/表格视图使用缩略图表示(ALAssetthumbnail 属性或resize the default representation 我自己)。如果我将此缩略图缓存在任何地方(例如您的问题建议的持久存储),那么我可以使用缩略图的高质量表示(我使用 PNG,因为它是无损压缩的,但质量因子为 0.7-0.9 的 JPEG 是也是相当忠实的版本)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-24
    • 2012-01-09
    • 2011-10-10
    • 1970-01-01
    • 2011-12-01
    • 2016-01-22
    相关资源
    最近更新 更多