Code optimization
I have a small app that displays screens from a remote PC. And as usually to improve performace we update only the parts of the screens that are changing. The app works but when the screen changes fast the update on IPAD is quite slow. Looking at the code in 'dawrect' and DoImage I see a lot of code duplicated, is there any way to optimize it ?
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,kCGImageAlphaPremultipl开发者_JAVA技巧iedLast);
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];
}
- (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);
}
Hope it helps a bit
精彩评论