使用图像时在旧设备上保存内存的提示

时间:2015-08-24 14:25:23

标签: ios swift memory parse-platform

在我的应用程序中,我需要显示几个我从Parse获取的图像,一切都很棒,直到我在iPad mini和iPad 2上测试...

为了给你一个更好的主意,我的主页是一个包含4个单元格的桌面视图。 第一个单元格必须显示“大”图像(50-55KB),第二个单元格显示缩略图图像的表格视图(5-8KB) - 但不超过5个集合视图单元格! - 其他2个单元格用于文本,按钮等。

我正在使用Parse作为后端,我开始使用它的工具来处理像PFImageView这样的图像:这种方式从我使用这两种方法的服务器加载图像:

theImageView.file = thePFObject["<PFFileEntity>"] as? PFFile
theImageView..loadInBackground()

一旦应用程序完成加载主页,使用的内存设备为100-110Mb,真的接近125-130Mb,因为系统关闭了应用程序。

我用以下代码替换了上面的行:

let imageData = NSData(contentsOfURL: NSURL(string: (stringContainingTheURL))!)
dispatch_async(dispatch_get_main_queue()) {
self.recipeImageView.image = UIImage(data: imageData!) }

,内存使用量现在为83-85Mb。更好但是​​当我进入另一个仅包含YTPlayerView(用于嵌入youTube视频)的视图和5个缩略图的集合视图时,它很容易关闭。

我减少记忆影响的策略是:  尽可能缩略图像;  避免UIImage(命名:)方法;  避免使用PFFiles:这样我将PFObjects存储到数组中,我使用字符串而不是文件;  调用didReciveMemoryWarnings()尽可能释放内存

我已经检查过没有内存泄漏,但我不知道如何改善行为。 有什么想法吗?

我会把你的前两个单元格的代码留给你,以便与你比较。

class HomeTableViewController: UITableViewController, UICollectionViewDelegate, UICollectionViewDataSource {

//MARK: -outlets

var collectionView: UICollectionView! { didSet {collectionView.reloadData() } }

// MARK: - Model
let indexPathforcell0 = NSIndexPath(forRow: 0, inSection: 0)
let indexPathforcell1 = NSIndexPath(forRow: 1, inSection: 0)

var firstCellObject:  PFObject? { didSet{ tableView.reloadRowsAtIndexPaths([indexPathforcell0], withRowAnimation: .Fade) } }
var secondCellArrayOfObjects = [PFObject]() { didSet { tableView.reloadRowsAtIndexPaths([indexPathforcell1], withRowAnimation: .Fade) } }

// MARK: - life cycle

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

override func viewDidLoad() {

    super.viewDidLoad()
    //fetch
    fetchFirstCellObject()
    fetchSecondCellArrayOfObjects()
}

 private func fetchFirstCellObject() {
//At the end...
// self.firstCellObject = something
}

private func fetchSecondCellArrayOfObjects() {
//self.secondCellArrayOfObjects = something

}

// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 4
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    var mainCell = UITableViewCell()
    switch indexPath.row {

    case 0:
        let cell = tableView.dequeueReusableCellWithIdentifier("Cell1", forIndexPath: indexPath) as! CustomTableViewCell
        cell.object = firstCellObject
        mainCell = cell

    case 1:
        var cell = self.tableView.dequeueReusableCellWithIdentifier("Cell2") as! UITableViewCell
        collectionView = cell.viewWithTag(1) as! UICollectionView
        collectionView.delegate = self
        collectionView.dataSource = self
        mainCell = cell


    return mainCell
}

// MARK: - UICollectionViewDataSource

func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
    return 1
}

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 5
}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    var cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! CommentedRecipeCollectionViewCell

    cell.object = secondCellArrayOfObjects[indexPath.row]

    return cell
}
}

这里我是如何实现UITableViewCell的:

class CustomTableViewCell: UITableViewCell {

// MARK: - Model

var object:  PFObject? { didSet { updateUI() } }

@IBOutlet weak var objectImageView: PFImageView!
@IBOutlet weak var objectLabel: UILabel!


private func updateUI() {
    let qos = Int(QOS_CLASS_USER_INITIATED.value)
    dispatch_async(dispatch_get_global_queue(qos, 0)) {
        let URLToUse = // ....
        if let stringToUse = theString {
            let imageData = NSData(contentsOfURL: NSURL(string: (URLToUse))!)
            dispatch_async(dispatch_get_main_queue()) {
                self.homeImageView.image = UIImage(data: imageData!)
            }
         }
    }

    objectLabel.text = // ....
}
}

0 个答案:

没有答案