0

我有一个动画时钟,目前存在内存泄漏。我目前似乎能够将东西放在图像上,但不能将其拿走或必须清除它(除了重新绘制整个时钟,因此秒针不在两个不同的位置)。

用于清除时钟组件的代码是:

for(UIView *image in self.toWipe)
{
    [image removeFromSuperview];
}
[self.toWipe removeAllObjects];

绘制秒针的代码(小时和分钟的处理相同)是:

CGAffineTransform secondTransform = CGAffineTransformMakeTranslation(centerX, centerY);
secondTransform = CGAffineTransformTranslate(secondTransform, -10, -189);
secondTransform = CGAffineTransformRotate(secondTransform, seconds / 60.0 * M_PI * 2.0);
UIImageView *secondHandView = [[UIImageView alloc] initWithImage:secondHandImage];
secondHandView.transform = secondTransform;
[self.view addSubview:secondHandView];
[self.toWipe addObject:secondHandView];
[self.toWipe addObject:secondHandImage];

我已经通过在每次迭代中绘制钟面来掩盖绘制的秒针而不是自行擦除;如果我不这样做,秒针(或其他)将扫出越来越多的馅饼,直到它们达到 360° 填充圆盘。记忆缓慢而稳定地增长,秒针的运动变得更加不稳定。

还有什么我可以或应该做的事情来将用过的物品交给遗忘吗?我在这个项目中使用 ARC;我想知道我还能提供更多或不同的方法,以便修复内存泄漏。

谢谢,

- 编辑 -

以下是方法体,其中 cruft 被注释掉了,而 cruft 没有被注释掉。它不处理异常,并且不会从正文中的任何内容引发异常:

int height = floor([[UIScreen mainScreen] bounds].size.height + .4);
int width = floor([[UIScreen mainScreen] bounds].size.width + .4);
UIImage *backgroundImage = nil;

if (height == 2048 || height == 2008 || height == 1024 || height == 1004 || height == 984)
{
    if (UIInterfaceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation))
    {
        backgroundImage = [UIImage imageNamed:@"Background-Default-Portrait.png"];
    }
    else
    {
        backgroundImage = [UIImage imageNamed:@"Background-Default-Landscape.png"];
    }
}
else if (height == 1536 || height == 768 || height == 748 || height == 728)
{
    backgroundImage = [UIImage imageNamed:@"Background-Default-Landscape.png"];
}
else if (height == 1136 || height == 1116 || height == 1096)
{
    if (UIInterfaceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation))
    {
        backgroundImage = [UIImage imageNamed:@"Background-Default-568.png"];
    }
    else
    {
        backgroundImage = [UIImage imageNamed:@"Background-Default-Rotated-568.png"];
    }
}
else if (height == 960 || height == 940 || height == 920 || height == 480 || height == 460 || height == 440)
{
    if (UIInterfaceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation))
    {
        backgroundImage = [UIImage imageNamed:@"Background-Default.png"];
    }
    else
    {
        backgroundImage = [UIImage imageNamed:@"Background-Default-Rotated.png"];
    }
}
else if ((height == 640 || height == 620 || height == 600) && (width == 1136 || width == 1116 || width == 1096))
{
    backgroundImage = [UIImage imageNamed:@"Background-Rotated-568.png"];
}
else if ((height == 640 || height == 620 || height == 600 || height == 320 || height == 300 || height == 280) && (width == 960 || width == 940 || width == 920 || width == 480 || width == 470 || width == 410))
{
    if (UIInterfaceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation))
    {
        backgroundImage = [UIImage imageNamed:@"Background-Default-Portrait.png"];
    }
    else
    {
        backgroundImage = [UIImage imageNamed:@"Background-Default-Rotated.png"];
    }
}
else
{
    backgroundImage = [UIImage imageNamed:@"Background-Default-Portrait.png"];

}
backgroundImage = [UIImage imageNamed:@"Background-Default-Landscape.png"];


if (!UIInterfaceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation))
{
    int juggle = height;
    height = width;
    width = juggle;
}
NSUInteger centerX = width * .5;
NSUInteger centerY = height * .5;

CGRect containerRect = CGRectZero;
containerRect.size = [[UIScreen mainScreen] bounds].size;

for(UIView *image in self.toWipe)
{
    [image removeFromSuperview];
}
[self.toWipe removeAllObjects];
self.view.backgroundColor = [UIColor colorWithPatternImage:backgroundImage];
//     UIView *background = [UIImage imageNamed:@"Background-Default-Landscape.png"];
UIImageView *backgroundView = [[UIImageView alloc] initWithImage:backgroundImage];
[self.view addSubview:backgroundView];
[self.toWipe addObject:backgroundView];
self.containerView = [[UIView alloc] initWithFrame:containerRect];
// float hours = 5.0;
// float minutes = 19.0;
// float seconds = 312.0;
// int timeStampMilliseconds = (ceil([[NSDate date] timeIntervalSince1970] * 1000.0));
// double timeStampMilliseconds = [[NSDate date] timeIntervalSince1970] * 1000.0;
double timeStampMilliseconds = CACurrentMediaTime() * 1000;
double hours = fmodf(timeStampMilliseconds, 86400000) / 3600000.0;
double minutes = fmodf(timeStampMilliseconds, 3600000) / 60000.0;
double seconds = fmodf(timeStampMilliseconds, 60000) / 1000.0;
// NSLog(@"Milliseconds: %lf, Hours: %lf, minutes: %lf, seconds: %lf", timeStampMilliseconds, hours, minutes, seconds);
[self.containerView removeFromSuperview];
self.containerView = [[UIView alloc] initWithFrame:containerRect];
UIImage *hourHandImage = [UIImage imageNamed:@"hour-hand.png"];
UIImageView *hourHandView = [[UIImageView alloc] initWithImage:hourHandImage];
hourHandImage = [UIImage imageNamed:@"hour-hand.png"];
hourHandView = [[UIImageView alloc] initWithImage:hourHandImage];
[self.view addSubview:hourHandView];

// UIImage *dotImage = [UIImage imageNamed:@"dot.png"];
// UIImageView *dotView = [[UIImageView alloc] initWithImage:dotImage];
// dotView.transform = CGAffineTransformMakeTranslation(centerX, centerY);
// [self.view addSubview:dotView];



hourHandView.layer.anchorPoint = CGPointMake(0.5f, 0.5f);
CGAffineTransform hourTransform = CGAffineTransformMakeTranslation(centerX, centerY);
    hourTransform = CGAffineTransformTranslate(hourTransform, -22, -122);
    hourTransform = CGAffineTransformRotate(hourTransform, hours / 12.0 * M_PI * 2.0);
    CGAffineTransform minuteTransform = CGAffineTransformMakeTranslation(centerX, centerY);
    minuteTransform = CGAffineTransformTranslate(minuteTransform, -10, -182);
    minuteTransform = CGAffineTransformRotate(minuteTransform, minutes / 60.0 * M_PI * 2.0);
    // hourTransform = CGAffineTransformTranslate(hourTransform, -sin(angle) * 50 + 122 , -cos(angle) * 50 + 122);


// hourTransform = CGAffineTransformTranslate(hourTransform, -330, 330);
hourHandView.transform = hourTransform;
[self.toWipe addObject:hourHandImage];
[self.toWipe addObject:hourHandView];
UIImage *minuteHandImage = [UIImage imageNamed:@"minute-hand.png"];
UIImageView *minuteHandView = [[UIImageView alloc] initWithImage:minuteHandImage];
minuteHandView.transform = minuteTransform;
[self.view addSubview:minuteHandView];
[self.toWipe addObject:minuteHandView];
[self.toWipe addObject:minuteHandImage];

minuteTransform = CGAffineTransformRotate(minuteTransform, minutes / 60.0 * M_PI * 2.0);
UIImage *secondHandImage = [UIImage imageNamed:@"second-hand.png"];
CGAffineTransform secondTransform = CGAffineTransformMakeTranslation(centerX, centerY);
secondTransform = CGAffineTransformTranslate(secondTransform, -10, -189);
secondTransform = CGAffineTransformRotate(secondTransform, seconds / 60.0 * M_PI * 2.0);
UIImageView *secondHandView = [[UIImageView alloc] initWithImage:secondHandImage];
secondHandView.transform = secondTransform;
[self.view addSubview:secondHandView];
[self.toWipe addObject:secondHandView];
[self.toWipe addObject:secondHandImage];



// hourHandView.layer.anchorPoint = CGPointMake(centerX, centerY);
// hourHandView.layer.affineTransform = CGAffineTransformRotate(transform, hour / (12.0 * M_PI * 2.0));
// UIImageView *minuteHandView = [[UIImageView alloc] initWithImage:minuteHandImage];
// UIImageView *secondHandView = [[UIImageView alloc] initWithImage:secondHandImage];

// [self.view addSubview:minuteHandView];
// [self.view addSubview:secondHandView];
4

1 回答 1

2

您是否尝试过每秒钟应用一次变换,而不是一遍又一遍地删除和添加图像视图?

这是我用于我正在开发的时钟应用程序的代码:

-(void)updateClock
{
    NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components:(NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:inDate];
    NSInteger seconds = [dateComponents second];
    NSInteger minutes = [dateComponents minute];
    NSInteger hours = [dateComponents hour];
    //NSLog(@"raw: hours:%d min:%d secs:%d", hours, minutes, seconds);
    if (hours > 12) hours -=12; //PM

    //set angles for each of the hands
    CGFloat secAngle = Degrees2Radians(seconds/60.0*360);
    CGFloat minAngle = Degrees2Radians(minutes/60.0*360);

    CGFloat minAdjustment = minAngle/12.0;
    CGFloat hourAngle = Degrees2Radians(hours/12.0*360) + minAdjustment;

    //reflect the rotations + 180 degres since CALayers coordinate system is inverted
    secHand.transform = CATransform3DMakeRotation (secAngle+M_PI, 0, 0, 1);
    minHand.transform = CATransform3DMakeRotation (minAngle+M_PI, 0, 0, 1);
    hourHand.transform = CATransform3DMakeRotation (hourAngle+M_PI, 0, 0, 1);
}
于 2013-10-11T23:50:27.340 回答