iOS - 不会一次分配太多内存 [英] iOS - Not allocating too much memory at once

查看:38
本文介绍了iOS - 不会一次分配太多内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试解决某些 iOS 设备上发生的崩溃问题,同时结合 Apple 提出的不要导致分配高峰"的建议.如何更改此代码以使其不立即发生?

Trying to get around a crash that is happening on some iOS devices, in conjunction with advice from Apple to "not cause allocation spikes". How can I change this code to not happen all at once?

for (Item *item in self.items) {
        ItemView *itemView = [[ItemView alloc] initWithFrame:CGRectMake(xPos, kYItemOffsetIphone, kItemWidthIphone, kItemHeightIphone) ];

        itemView.delegate = self;
        [itemView layoutWithData:item]; //this just adds an imageView and button
        [self.scrollView addSubview:itemView];
        xPos += kXItemSpacingIphone;
    }

self.items 数组中大约有 20 个对象,用于构建 20 个 ItemView.再说一次,有没有办法让这段代码不那么分配密集"?

There are around 20 objects in the self.items array, which are used to build the 20 ItemViews. Again, is there some way to make this code less "allocation intensive"?

推荐答案

我个人做了一些类似的事情:

I personally do something along the lines of:

  1. 让我的视图控制器成为滚动视图的 delegate(如果你在代码中这样做,你必须修改你的视图控制器的 .h 以说它符合 UIScrollViewDelegate).

  1. Make my view controller the delegate of the scroll view (if you do this in code, you have to modify your view controller's .h to say that it conforms to UIScrollViewDelegate).

定义一个 scrollViewDidScroll 方法(a)确定滚动视图可见部分的框架;(b) 确定哪些子视图与该可见部分相交;(c) 加载可见的项目,卸载不可见的项目.

Define a scrollViewDidScroll method that (a) determines the frame of the visible portion of the scroll view; (b) determine which of the subviews intersect with that visible portion; (c) load the items that are visible, and unload the ones that aren't.

例如,它可能如下所示:

So, for example, it might look something like the following:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    // Determine the frame of the visible portion of the scrollview.

    CGRect visibleScrollViewFrame = scrollView.bounds;
    visibleScrollViewFrame.origin = scrollView.contentOffset;

    // Now iterate through the various items, remove the ones that are not visible,
    // and show the ones that are.

    for (Item *itemObject in self.itemCollection)
    {
        // Determine the frame within the scrollview that the object does (or 
        // should) occupy.

        CGRect itemObjectFrame = [self getItemObjectFrame:itemObject];

        // see if those two frames intersect

        if (CGRectIntersectsRect(visibleScrollViewFrame, itemObjectFrame))
        {
            // If it's visible, then load it (if it's not already).
            // Personally, I have my object have a boolean property that
            // tells me whether it's loaded or not. You can do this any
            // way you want.

            if (!itemObject.loaded)
                [itemObject loadItem];
        }
        else
        {
            // If not, go ahead and unload it (if it's loaded) to conserve memory.

            if (itemObject.loaded)
                [itemObject unloadItem];
        }
    }
}

这是基本的想法.您当然可以根据应用的特定设计优化此逻辑,但我通常是这样做的.

That's the basic idea. You can certainly optimize this logic based upon your app's particular design, but this is how I generally do it.

这篇关于iOS - 不会一次分配太多内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆