나는 그래프를 자주 그리는 프로그램을 작성 중이다. 불행히도 그것은 어떤 이유로 코어 그래픽이 이전 그래픽을 풀어 놓는 것처럼 보이지 않기 때문에 메모리 문제를 빨리 겪게됩니다. 각 추첨이 끝날 때 어떻게 강제로 메모리를 해제 할 수 있습니까?iphone의 코어 그래픽 메모리 사용량 줄이기
해당보기를 닫을 때 메모리가 해제되므로 메모리 누수가 아닙니다. NSTimer를 코드의 여러 위치로 이동시켜 보았습니다.
아래의 코드는 파일이 가장 중요한 것부터 (잠재적으로) 관련성이없는 것입니다.
먼저 그래픽 (이 경우 사인파)을 매번 그리는 코어 파일 + 헤더가 있습니다.
GraphicsView.h
#import <UIKit/UIKit.h>
#import "Model.h"
@interface GraphicsView : UIView
{
Model * model;
}
@end
GraphicsView.m
#import "GraphicsView.h"
@implementation GraphicsView
- (id)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame])
{
model=[Model sharedModel];
[NSTimer scheduledTimerWithTimeInterval:.010 target:self selector:@selector(setNeedsDisplay) userInfo:nil repeats:YES];
}
return self;
}
- (void)drawRect:(CGRect)rect
{
int now=[model n];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextClearRect(context, CGRectMake(0, 0, rect.size.width, rect.size.height));
CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
CGContextMoveToPoint(context, 0.0, 0.0);
double xc_old=0;
double yc_old=100;
for (int i=0;i<now;i++)
{
double xc=(double)i/(double)now*200;
double yc=100-100*sin((double)i/6);
CGContextMoveToPoint(context, xc_old, yc_old);
CGContextAddLineToPoint(context, xc, yc);
CGContextStrokePath(context);
xc_old=xc;
yc_old=yc;
}
}
- (void)dealloc {
[super dealloc];
}
@end
뷰 컨트롤러 헤더 :
#import <UIKit/UIKit.h>
#import "GraphicsView.h"
@interface SbarLeakAppDelegate : NSObject <UIApplicationDelegate>
{
UIWindow *window;
Model *model;
GraphicsView * gv;
NSTimer * timer;
}
@end
뷰 컨트롤러 자체.
#import "SbarLeakAppDelegate.h"
@implementation SbarLeakAppDelegate
- (void)applicationDidFinishLaunching:(UIApplication *)application
{
model=[Model sharedModel];
CGRect appRect;
CGRect frame = CGRectInset(appRect, 0.0f, 0.0f);
appRect.origin=CGPointMake(0.0, 40.0);
appRect.size = CGSizeMake(200.0f, 200.0f);
frame = CGRectInset(appRect, 0.0f, 0.0f);
gv=[[GraphicsView alloc] initWithFrame:appRect];
[gv setFrame:frame];
[window addSubview:gv];
[gv release];
[window makeKeyAndVisible];
}
-(void) viewDidAppear:(id)sender
{
//timer=[NSTimer scheduledTimerWithTimeInterval:.250 target:gv selector:@selector(setNeedsDisplay) userInfo:nil repeats:YES];
}
- (void)dealloc
{
[window release];
[super dealloc];
}
@end
이 코드는 완전성을 위해 포함되었지만 질문에 영향을 주어서는 안됩니다. 데이터 소스 헤더 파일 (단순한 정수)
#import <UIKit/UIKit.h>
@interface Model : NSObject
{
int n;
}
@property int n;
+(Model *) sharedModel;
-(void) inc;
@end
데이터 소스 Model.m :
#import "Model.h"
@implementation Model
static Model * sharedModel = nil;
+ (Model *) sharedModel
{
if (sharedModel == nil)
sharedModel = [[self alloc] init];
return sharedModel;
}
@synthesize n;
-(id) init
{
self=[super init];
[NSTimer scheduledTimerWithTimeInterval:.05 target:self selector:@selector(inc) userInfo:nil repeats:YES];
return self;
}
-(void) inc
{
n++;
}
@end
해결 방법을 찾으셨습니까? –