我有一個平移手勢,可以在一系列圖像上平移。我不知道如何正確地管理這個內存,經過一段時間的平移之後,我得到了崩潰。加載圖像中的內存管理
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