- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个动画时钟,目前有内存泄漏。我目前似乎能够将东西放在图像之上,但不能将其取走或必须清除它(除了重新绘制整个时钟以使秒针不在两个不同的位置之外)。
用于清除时钟组件的代码是:
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];
最佳答案
您是否尝试过每秒都应用变换,而不是一遍又一遍地删除和添加 ImageView ?
这是我用于我正在开发的时钟应用程序的代码:
-(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);
}
关于ios - 如何删除屏幕上绘制的图像并释放所有链接以便 ARC 回收?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19328776/
我有一个附加了 View Controller 的 AVAudioPlayer 实例。 @property (nonatomic, retain) AVAudioPlayer *previewAudi
我是java初学者。假设我声明了一个 Account 类型的变量 Account _account = new Account("Thomas"); 然后在其他地方我做了这样的事情: _account
我在我的应用程序中使用了 3 个 UIViewController,现在我想知道当我从另一个应用程序切换到另一个 UIViewController 时释放它们是否是一个好主意。显然,这将是隐藏的,当它
我分配了一个直接缓冲区: ByteBuffer directBuffer = ByteBuffer.allocateDirect(1024); 我读过: Deallocating Direct Buf
场景。我有一个图表,我可以使用右键单击来执行平移。这非常有效。然后我完美地添加了右键菜单。 问题。现在,即使在拖动操作完成后释放鼠标,也会显示右键菜单。 有没有办法在 Java Swing 或 Jav
我使用此代码获取 ABPerson 的姓氏 CFStringRef lastNameRef = ABRecordCopyValue((ABRecordRef)personRecordRef, kABP
目前,我们在基于 C 的嵌入式应用程序中使用 malloc/free Linux 命令进行内存分配/取消分配。我听说这会导致内存碎片,因为内存分配/取消分配会导致堆大小增加/减少,从而导致性能下降。其
当我尝试释放缓冲区时遇到问题。每次我尝试将缓冲区传递给释放方法时,都会发生段错误。 Valgrind 确认段错误位于 BufferDeallocate 方法中。 ==30960== Memcheck,
我想知道何时按下或释放修改后的键(Ctrl 或 Shift)。 基本上,用户可以在按下修改键的情况下执行多次击键,而我不想在它被释放之前执行一个操作(想想 Emacs 和 Ctrl + X + S).
我编写了一个相当大的网络应用程序。它运行良好一段时间,然后慢慢开始运行缓慢,因为 DOM 节点开始爬升到 80,000 - 100,000 左右。 所以我一直在 Chrome 开发工具控制台 (DCT
我知道在像 c 这样的语言中,我需要在分配内存后释放它。 (我来自 Java),对此我有几个问题: 当我在做的时候: int array[30]; (即创建一个大小为 30 个整数的数组)与
这个问题在这里已经有了答案: 关闭 11 年前。 Possible Duplicate: How to release pointer from boost::shared_ptr? Detach
我有一个可以从多个后台线程访问的类,可能同时访问。我无法复制该类,因为重新创建它的内容(处理或内存方面)可能很昂贵。 也有可能在后台处理仍在继续并访问该属性时替换了此类的属性。 目前我有定期的保留/释
这个问题是对: 的扩展链接-1:Creating an image out of the ios surface and saving it Link-2:Taking Screenshots fro
我有一个实例变量 NSMutableArray* searchResults。 首先,我初始化它: self.searchResults = [[NSMutableArray alloc] init]
如果我在堆上声明一些东西,比如 char *a=new char[1000] 并且主程序停止,如果没有 delete[]<,那么分配的内存会发生什么 调用?它保留在堆上还是自动释放? 最佳答案 就C+
在开发相机应用时,我遇到了一个异常,该异常仅在我切换到其他应用时发生(onPause() 用于我的应用)。 01-15 17:22:15.017: E/AndroidRuntime(14336): F
使用 JDK 1.8 编译时出现 maven 编译器错误 无法执行目标 org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (de
将 BufferedImage 保存到磁盘(以释放内存)的最快方法是什么? 我的 Java 应用程序处理大量图像(每约 300 毫秒将图像加载到内存中)。大多数这些图像都会立即被丢弃 (gc),但每隔
使用 JDK 1.8 编译时出现 maven 编译器错误 未能在项目 DUMMY 上执行目标 org.apache.maven.plugins:maven-compiler-plugin:3.8.1:
我是一名优秀的程序员,十分优秀!