2013-03-28 2 views
1

기본 화면SetNeedsDisplay method이 기록 된 응용 프로그램을 개발 중입니다.DrawRect 메서드에서받은 메모리 경고

그러나 문제는 많은 메모리가 필요하며 심지어 화면을 녹화하지 않는다는 것입니다.

아래 언급 된 코드의 메모리 사용을 줄이려고합니다.

이 문제에 대한 해결책은 무엇입니까?

미리 감사드립니다.

- (void) drawRect:(CGRect)rect 
{ 
    NSDate* start = [NSDate date]; 
    CGContextRef context = [self createBitmapContextOfSize:self.frame.size]; 
    //NSLog(@"context value %@",context); 
    //not sure why this is necessary...image renders upside-down and mirrored 
    CGAffineTransform flipVertical = CGAffineTransformMake(1, 0, 0, -1, 0, self.frame.size.height); 
    CGContextConcatCTM(context, flipVertical); 

    [self.layer renderInContext:context]; 

    CGImageRef cgImage = CGBitmapContextCreateImage(context); 
    UIImage* background = [UIImage imageWithCGImage: cgImage]; 
    CGImageRelease(cgImage); 
    self.currentScreen = background; 

    if (_recording) { 
     float millisElapsed = [[NSDate date] timeIntervalSinceDate:startedAt] * 1000.0; 
     [self writeVideoFrameAtTime:CMTimeMake((int)millisElapsed, 1000)]; 
    } 

    float processingSeconds = [[NSDate date] timeIntervalSinceDate:start]; 
    delayRemaining = (1.0/self.frameRate) - processingSeconds; 

    //redraw at the specified framerate 
    [self performSelector:@selector(setNeedsDisplay) withObject:nil afterDelay:delayRemaining > 0.0 ? delayRemaining : 0.01]; 
} 

-(CGContextRef) createBitmapContextOfSize:(CGSize) size 
{ 

    CGContextRef context = NULL; 
    CGColorSpaceRef colorSpace; 
    int    bitmapByteCount; 
    int    bitmapBytesPerRow; 

    bitmapBytesPerRow = (size.width * 4); 
    bitmapByteCount  = (bitmapBytesPerRow * size.height); 
    colorSpace = CGColorSpaceCreateDeviceRGB(); 
    if (bitmapData != NULL) { 
     free(bitmapData); 
    } 
    bitmapData = malloc(bitmapByteCount); 
    if (bitmapData == NULL) { 
     fprintf (stderr, "Memory not allocated!"); 
     return NULL; 
    } 

    context = CGBitmapContextCreate (bitmapData, 
            size.width, 
            size.height, 
            8,  // bits per component 
            bitmapBytesPerRow, 
            colorSpace, 
            kCGImageAlphaNoneSkipFirst); 

    CGContextSetAllowsAntialiasing(context,NO); 
    if (context== NULL) { 
     free (bitmapData); 
     fprintf (stderr, "Context not created!"); 
     return NULL; 
    } 
    CGColorSpaceRelease(colorSpace); 

    return context; 
} 
+0

그것은 당신이 결코 어쨌든보기 드로잉없는 것 같아, 그래서 왜 사용하지 NSTimer 또는 뭔가 대신? – jjv360

+0

'[self performSelector : @selector (setNeedsDisplay) withObject : nil afterDelay : delayRemaining> 0.0 대신 NSTimer를 사용하면 어떤 차이가 있습니까? delayRemaining : 0.01];'?? – user1673099

답변

2

당신은 컨텍스트 모든 프레임을 만들 수 없지만, 결코를 눌렀다 끝에이 추가 시도 :

CGContextRelease(context); 
+0

끝에이 줄을 추가하려고하면 메서드에서 누수에 대한 메시지를 표시합니다. 호출자가이 시점에 소유하지 않은 개체의 참조 횟수를 잘못 감소시킵니다. " – user1673099

+0

'createBitmapContextOfSize'의 포스트 코드? – jjv360

+0

내 코드를 업데이트했습니다. 이것을 봐주세요. – user1673099