首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >代码优化

代码优化
EN

Stack Overflow用户
提问于 2011-04-11 18:16:34
回答 1查看 444关注 0票数 1

我有一个小应用程序,可以显示来自远程PC的屏幕。通常,为了提高性能,我们只更新正在更改的屏幕部分。这款应用可以正常工作,但当屏幕快速变化时,IPAD上的更新速度会非常慢。看看'dawrect‘和DoImage中的代码,我发现有很多代码是重复的,有什么方法可以优化它吗?

Thx

代码语言:javascript
运行
复制
- (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];
}
EN

回答 1

Stack Overflow用户

发布于 2011-04-11 18:28:04

代码语言:javascript
运行
复制
- (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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5619854

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档