1

我是IOS开发的新手。在我的应用程序中,我有几张正在制作动画的图像。图像的数量可能会有所不同。

在 ipad 2 上运行时,动画效果很好。在 ipad 1 上运行时,有大量图像(20+),应用程序只是崩溃并出现内存警告。

我想提前计算动画将占用的内存量。根据这个数字,我可以计算出是完成我的动画还是跳到最终状态。

如何才能做到这一点?

编辑:

我当前的代码:

- (void)remix
{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:animationDuration];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:
     @selector(animationDidStop:finished:context:)];

    self.currentStatus = canvas_status_animating;
    NSMutableArray *circles = [[NSMutableArray alloc] init];

    for (CircleView* view in self.subviews)
    { 
        if(![view isKindOfClass:[CircleView class]])
            continue;
        [circles addObject:view];
    }

    [self animatePosition:circles];

    [UIView commitAnimations];
}


-(void) animatePosition:(NSArray*)circles
{
    int maxWidth = self.bounds.size.width; 
    int maxHeight = self.bounds.size.height;


    for (CircleView* view in circles)
    {  
        int selectedX = 0;
        int selectedY = 0;
        if ((arc4random()%200)+1 > 100)
        {
            selectedX = (arc4random() % maxWidth) + 1; 
            selectedY = (arc4random() % 200) + 1;
            selectedY = (selectedY > 100) ? (maxHeight - selectedY) : selectedY;
        }
        else 
        {
            selectedX = (arc4random() % 200) + 1; 
            selectedX = (selectedX > 100) ? (maxWidth - selectedX) : selectedX;
            selectedY = (arc4random() % maxHeight) + 1;
        }                                

        view.frame = CGRectMake(selectedX - view.frame.size.width / 2, 
                                selectedY - view.frame.size.height / 2,
                                view.frame.size.width, 
                                view.frame.size.height);

    }

}
4

1 回答 1

3

您可以在前后调用此函数,并计算差异。

-(double)availableMemory
{
    vm_statistics_data_t vmStats;
    mach_msg_type_number_t infoCount = HOST_VM_INFO_COUNT;
    kern_return_t kernReturn = host_statistics(mach_host_self(), HOST_VM_INFO, (host_info_t)&vmStats, &infoCount);

    if (kernReturn != KERN_SUCCESS)
    {
        return NSNotFound;
    }

    return ((vm_page_size * vmStats.free_count) / 1024.0) / 1024.0;

}
于 2012-07-13T00:21:04.913 回答