기본 화면 및 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;
}
그것은 당신이 결코 어쨌든보기 드로잉없는 것 같아, 그래서 왜 사용하지 NSTimer 또는 뭔가 대신? – jjv360
'[self performSelector : @selector (setNeedsDisplay) withObject : nil afterDelay : delayRemaining> 0.0 대신 NSTimer를 사용하면 어떤 차이가 있습니까? delayRemaining : 0.01];'?? – user1673099