2009-05-21 2 views
2

현재 iPhone 게임을 진행 중이며 즉시 애니메이션을 만들어야합니다. 애니메이션은 여러 이미지를 기반으로하며 이미지 조합의 범위가 너무 커서 모든 시퀀스를 미리 만들 수 없으므로 상황이 바뀔 때마다 이미지 세트를 다시 계산할 수 있기를 바랍니다.이미지 생성 기능에서 메모리 누수가 발생했습니다.

이 함수는 애니메이션 시퀀스로 사용할 일련의 이미지를 만듭니다. 나는 두 가지 도움이 필요 : 내가 찾는 데 문제가 있어요 여러 대형 메모리 누수가 있습니다

  1. - 나는 아이폰에 새로 온 사람 때문에
  2. 것은, 내가 알고 싶어 내가 그들을 찾는 데 도움이 필요가 당신이 그것을 작성하는 방법에 대한 더 나은 제안이있는 경우는, 공유하시기 바랍니다 할 수있는 더 좋은 방법은 ...입니다

코드 :

(void) updateImages:(int) smallPicID 
{ 
    CGRect smallPicRect; 
    smallPicRect.size = CGSizeMake(25.0f, 25.0f); 

    //TODO: add smallPic location for every image. Currently the values only match the first image... 
    static const CGPoint smallPicLocation[11][SMALLPIC_LOCATION_COUNT] = 
    { 
     { {126.0f, 153.0f},{105.0f, 176.0f}, {115.0f, 205.0f}, {145.0f, 211.0f}, {166.0f, 188.0f}, {156.0f, 159.0f} }, 
     { {183.0f, 91.0f}, {201.0f, 97.0f}, {205.0f, 115.0f}, {191.0f, 127.0f}, {173.0f, 122.0f}, {169.0f, 124.0f} }, 
     { {183.0f, 91.0f}, {201.0f, 97.0f}, {205.0f, 115.0f}, {191.0f, 127.0f}, {173.0f, 122.0f}, {169.0f, 124.0f} }, 
     { {183.0f, 91.0f}, {201.0f, 97.0f}, {205.0f, 115.0f}, {191.0f, 127.0f}, {173.0f, 122.0f}, {169.0f, 124.0f} }, 
     { {183.0f, 91.0f}, {201.0f, 97.0f}, {205.0f, 115.0f}, {191.0f, 127.0f}, {173.0f, 122.0f}, {169.0f, 124.0f} }, 
     { {183.0f, 91.0f}, {201.0f, 97.0f}, {205.0f, 115.0f}, {191.0f, 127.0f}, {173.0f, 122.0f}, {169.0f, 124.0f} }, 
     { {183.0f, 91.0f}, {201.0f, 97.0f}, {205.0f, 115.0f}, {191.0f, 127.0f}, {173.0f, 122.0f}, {169.0f, 124.0f} }, 
     { {183.0f, 91.0f}, {201.0f, 97.0f}, {205.0f, 115.0f}, {191.0f, 127.0f}, {173.0f, 122.0f}, {169.0f, 124.0f} }, 
     { {183.0f, 91.0f}, {201.0f, 97.0f}, {205.0f, 115.0f}, {191.0f, 127.0f}, {173.0f, 122.0f}, {169.0f, 124.0f} }, 
     { {183.0f, 91.0f}, {201.0f, 97.0f}, {205.0f, 115.0f}, {191.0f, 127.0f}, {173.0f, 122.0f}, {169.0f, 124.0f} }, 
     { {183.0f, 91.0f}, {201.0f, 97.0f}, {205.0f, 115.0f}, {191.0f, 127.0f}, {173.0f, 122.0f}, {169.0f, 124.0f} } 
    }; 

    for(int i = 0; i < self.m_finalImages.count; ++i) 
    { 
     // start with base image 
     UIImage * baseImg = [self.m_finalImagesEmpty objectAtIndex:i]; 
     CGImageRef baseImgCtx = baseImg.CGImage; 

     // Create the bitmap context that matches the baseImg parameters 
     CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 
     CGContextRef newImgCtx = CGBitmapContextCreate(NULL, 
                 CGImageGetWidth(baseImgCtx), 
                 CGImageGetHeight(baseImgCtx), 
                 8, 0, colorSpace, 
                 (kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst)); 
     CGColorSpaceRelease(colorSpace); 

     if (newImgCtx == NULL) 
     { 
      // error creating context 
      assert(0); 
      return; 
     } 

     // Get empty drum image width, height. 
     size_t w = CGImageGetWidth(baseImgCtx); 
     size_t h = CGImageGetHeight(baseImgCtx); 
     CGRect rect = {{0,0},{w,h}}; 

     // Draw the image to the bitmap context. 
     // newImgCtx now contains the full empty drum image. 
     CGContextDrawImage(newImgCtx, rect, baseImgCtx); 

     CGContextSaveGState(newImgCtx); 
     // translate & scale because of origin difference between UIKit and CG 
     CGContextTranslateCTM(newImgCtx, 0, h); 
     CGContextScaleCTM(newImgCtx, 1, -1); 

     int startLocation = 0; 
     int endLocation = SMALLPIC_LOCATION_COUNT; 

     // for each location 
     for(int j = startLocation; j < endLocation; j++) 
     { 
      // if its empty, nothing to do, move to next bullet 
      if (m_locationStatus[j] == CY_EMPTY) 
       continue; 

      UIImage * srcImg; 
      switch (m_locationStatus[j]) 
      { 
       case CY_FULL: srcImg = [self.m_finalImagesLoaded objectAtIndex:i]; break; 
       case CY_USED: srcImg = [self.m_finalImagesSpent objectAtIndex:i]; break; 
      } 

      // create small image containing only the smallpic from the src img 
      smallPicRect.origin = smallPicLocation[i][j]; 
      CGImageRef smallpicCGImg = CGImageCreateWithImageInRect(srcImg.CGImage, smallPicRect); 

      // draw the smallpic into the new context 
      CGContextDrawImage(newImgCtx, smallPicRect, smallpicCGImg);   

      CGImageRelease(smallpicCGImg); 
     } 

     CGContextRestoreGState(newImgCtx); 

     // update the image from the context 
     UIImage *baseImg = [self.m_finalImages objectAtIndex:i]; 
     CGImageRef imgref = CGBitmapContextCreateImage(newImgCtx); 
     //[baseImg release]; 
     [baseImg initWithCGImage:imgref]; 

     CGContextRelease(newImgCtx); 
    } 
} 
+0

다음은 Apple 사이트의 관련 스레드입니다. http://discussions.apple.com/thread.jspa?threadID=1664673 – Demi

답변

2

나를 뛰어 첫 번째 일들이 있습니다 imgref를 가까이에서 만들자. CGBitmapContextCreateImage()가있는 끝이지만 일치하는 CGImageRelease()가없고 baseImg initWithCGImage : 메시지가 메모리를 씹지 만 않습니다. 난 당신이 같은 뭔가 "컨텍스트에서 이미지를 업데이트"로 시작하는 부분을 교체 할 생각 :

CGImageRef imgref = CGBitmapContextCreateImage(newImgCtx); 
UIImage *replacmentBaseImg = [[UIImage alloc] initWithCGImage:imgref]; 
CGImageRelease(imgref); 

[self.m_finalImages replaceObjectAtIndex:i withObject:replacementBaseImg]; 
[replacementBaseImg release]; 

CGContextRelease(newImgCtx); 

이 m_finalImages가있는 NSMutableArray 있다고 가정하고 적절하게 삽입 된 다른 이미지를 출시했다고 그 배열에서, 그래서 당신이 그들을 대체 할 때 그들은 할당 해제 얻을.

큰 구조 메모에서는 더 작은 하위 이미지를 개별 CALayers로 그려보고 애니메이션을 완성하기 위해 여러 위치의 화면 안팎으로 해당 레이어를 바꿀 수 있습니다. 석영 드로잉은 비용이 많이 들며, CALayers는 캐시 된 이미지로 GPU에 텍스처로 저장됩니다. 다른 사람들은 CALayers를 사용하여 이러한 스프라이트 기반 애니메이션을 수행하고 인상적인 성능을 달성했습니다.

관련 문제