2011-04-11 164 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