2011-04-11 281 views
1

我有一個小應用程序,顯示來自遠程PC的屏幕。通常爲了提高性能,我們只更新正在改變的屏幕部分。該應用程序的作品,但是當屏幕變化很快時,IPAD上的更新速度很慢。看看'dawrect'和DoImage中的代碼我看到很多代碼重複,有沒有什麼辦法可以優化它?代碼優化

THX

- (id)initWithFrame:(CGRect)frame { 

    self = [super initWithFrame:frame]; 
    if (self) { 
     // Initialization code. 
    } 
    return self; 
} 

// Only override drawRect: if you perform custom drawing. 
- (void)drawRect:(CGRect)rect { 
    CGColorSpaceRef color = CGColorSpaceCreateDeviceRGB(); 
    context = CGBitmapContextCreate(offscreenPixels,1024,768,8,4*(1024),color,kCGImageAlphaPremultipliedLast); 
    UIGraphicsPushContext(context); 
    image = CGBitmapContextCreateImage(context); 
    UIGraphicsPopContext(); 
    CGContextRelease(context); 
    CGColorSpaceRelease(color); 

    CGContextRef c=UIGraphicsGetCurrentContext(); 
    CGContextDrawImage(c, CGRectMake(0, 0, 1024, 768), image); 

    CGImageRelease(image); 
} 

- (void)dealloc { 
    [super dealloc]; 
} 

- (void)DoImage:(CGRect)rect theImage:(UIImage*)aImage { 

    CGColorSpaceRef color = CGColorSpaceCreateDeviceRGB(); 
    context = CGBitmapContextCreate(offscreenPixels,1024,768,8,4*(1024),color,kCGImageAlphaPremultipliedLast); 
    UIGraphicsPushContext(context); 
    [aImage drawInRect:rect]; 
    UIGraphicsPopContext();  
    CGContextRelease(context); 
    CGColorSpaceRelease(color); 

    // Rfresh screen 
    [self setNeedsDisplayInRect:rect]; 
} 

回答

0
- (void)drawRect:(CGRect)rect { 
    [self doUpdateWithImage:image]; 
    CGContextRef c=UIGraphicsGetCurrentContext(); 
    CGContextDrawImage(c, CGRectMake(0, 0, 1024, 768), image); 

    CGImageRelease(image); 
} 

- (void)dealloc { 
    [super dealloc]; 
} 

- (void)DoImage:(CGRect)rect theImage:(UIImage*)aImage { 

    [self doUpdateWithImage:aImage]; 

    // Refresh screen 
    [self setNeedsDisplayInRect:rect]; 
} 

- (void)doUpdateWithImage:(UIImage *)image 
{ 
CGColorSpaceRef color = CGColorSpaceCreateDeviceRGB(); 
    context = CGBitmapContextCreate(offscreenPixels,1024,768,8,4*(1024),color,kCGImageAlphaPremultipliedLast); 
    UIGraphicsPushContext(context); 
    image = CGBitmapContextCreateImage(context); 
    UIGraphicsPopContext(); 
    CGContextRelease(context); 
    CGColorSpaceRelease(color); 
} 

希望它可以幫助一點

+0

蔭不知道這一點,但你可以嘗試更換[自doUpdateWithImage:aImage]。在圖像= aImage的DoImage中; – visakh7 2011-04-11 10:35:42

+0

Thx Bavarious,我會測試,但似乎沒有調用drawrect。 – user701934 2011-04-11 11:16:19