我有一个平移手势,可以在一系列图像上平移。我不知道如何正确地管理这个内存,经过一段时间的平移之后,我得到了崩溃。加载图像中的内存管理
animImage是一个UIImageView;
这里是它如何工作的:
- (IBAction) panAnim:(UIPanGestureRecognizer *)sender{
CGPoint translate = [sender translationInView:sliderView];
if (translate.x >= lastPoint) {
difference = translate.x - lastPoint;
[self forwardAnim:difference];
} else {
difference = lastPoint - translate.x;
[self backwardAnim:difference];
}
lastPoint = translate.x;
}
-(void)forwardAnim:(CGFloat)speed{
NSInteger newFrame = currentFrame+speed;
currentFrame = newFrame;
if (currentFrame>=totalFrames) {
currentFrame=0;
}
NSString *newImagePath = [NSString stringWithFormat:@"%@", [currentAnimation objectAtIndex:currentFrame]];
animImage.image = [UIImage imageNamed:newImagePath];
}
-(void)backwardAnim:(CGFloat)speed{
NSInteger newFrame = currentFrame-speed;
currentFrame = newFrame;
if (currentFrame<0) {
currentFrame=(totalFrames-1);
}
NSString *newImagePath = [NSString stringWithFormat:@"%@", [currentAnimation objectAtIndex:currentFrame]];
animImage.image = [UIImage imageNamed:newImagePath];
}
动画检测的转换位置,并计算出什么“框架”的动画应该是,然后换出的图像。
我得到了一个非常流畅的动画,但很明显我导致崩溃,因为我没有正确管理内存。我收到内存警告,然后崩溃 - 但只有当我滚动浏览图像一段时间。
我需要找出预先加载100张图像的方法,所以我只能保留100张图像的内存。这很奇怪,因为图像在仪器IO输出中正确打开和关闭。
感谢您的帮助!
干杯,d
animImage是一个UIImageView吗?乍一看,我没有看到你发布的代码有什么问题,而不是currentAnimation的索引问题。你有堆栈跟踪吗? – csano 2011-06-13 17:10:34
是的animImage是一个UIImageView。如果这段代码很好,那么崩溃的原因可能就在别处。我会调查。干杯 – 2011-06-14 07:36:37
@ j0k问题的原因是内存不足警告,然后崩溃 - 这是因为动画。 – 2011-06-14 16:28:10