首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >iOS -绘图效率(滑块更改时更新)

问iOS -绘图效率(滑块更改时更新)
EN

Stack Overflow用户
提问于 2013-04-03 22:04:40
回答 1查看 328关注 0票数 1

我正在给iOS上的一张图片上色。我正在使用滑块选择颜色。

当我将滑块上的"Update Events“设置为continuous时,调用的函数会被大量调用(滑块从0到1535),因此用户界面的响应不是很快。

有没有办法让下面的代码更有效率?我意识到我每次调用这个函数都是在开始一个新的绘图上下文--我可以“保存”这个上下文并重用它吗?

提前谢谢。

代码语言:javascript
复制
- (IBAction)bodyColourChanged:(UISlider *)sender {
// get the UIColor from self.colourArray
UIColor *color = [self.colourArray objectAtIndex:sender.value];

UIImage *myImage = [UIImage imageNamed:@"body.png"];

// Begin a new image context to draw the coloured image onto
UIGraphicsBeginImageContext(self.bodyView.image.size);

// Get a reference to the context we created
CGContextRef context = UIGraphicsGetCurrentContext();

// Set the fill colour
//[[UIColor colorWithRed:color.CGColor green:green blue:blue alpha:1.0] setFill];
[color setFill];

// translate/flip the graphics context (for transforming from CG* coords to UI* coords
CGContextTranslateCTM(context, 0, self.bodyView.image.size.height);
CGContextScaleCTM(context, 1.0, -1.0);

// set the blend mode and the original image
CGContextSetBlendMode(context, kCGBlendModeOverlay);
CGRect rect = CGRectMake(0, 0, self.bodyView.image.size.width, self.bodyView.image.size.height);
CGContextDrawImage(context, rect, myImage.CGImage);

// Set a mask that matches the shape of the image, then draw (colour burn) a coloured rectangle
CGContextClipToMask(context, rect, self.bodyView.image.CGImage);
CGContextAddRect(context, rect);
CGContextDrawPath(context, kCGPathFill);

// Generate a new UIImage from the graphics context we drew onto
UIImage *colouredImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

self.bodyView.image = colouredImage;
}

编辑:我上色的图片很大。它是1541x2000像素,因为我希望能够在不损失质量的情况下放大。也许这就是问题所在。我会继续修修补补,看看我能找到什么。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-04-03 23:25:33

我不确定你的着色方法,但我已经解决了滑块的类似性能问题,只在很小的延迟后调用性能密集型方法(即用户暂停了滑动)。

创建一些类变量/属性来保存与时间相关的对象:

代码语言:javascript
复制
@property (nonatomic, strong) NSDate *sliderValueChangedDate;
@property (nonatomic, strong) NSTimer *sliderValueChangedTimer;

在连接到UISlider事件的方法中:

代码语言:javascript
复制
- (IBAction)sliderValueChanged:(id)sender {
    // Save the time the slider was changed.
    self.sliderValueChangedDate = [NSDate date];
    // Start a timer if it's not already running.
    if (!self.sliderValueChangedTimer) {
        self.sliderValueChangedTimer = [NSTimer scheduledTimerWithTimeInterval:0.3 target:self selector:@selector(checkIfImageShouldBeColoured:) userInfo:nil repeats:YES];
    }
}

然后,在您的checkIfImageShouldBeColoured:方法中,您可以查看该值在此期间是否发生了更改:

代码语言:javascript
复制
- (void)checkIfImageShouldBeColoured:(NSTimer *)timer {
    // Get how long has been elapsed since the slider was last changed.
    NSTimeInterval elapsed = -[self.sliderValueChangedDate timeIntervalSinceNow];
    // If this is over our threshold, then perform the intensive method.
    if (elapsed > 0.3) {
        [self.sliderValueChangedTimer invalidate];
        self.sliderValueChangedTimer = nil;
        [self changeBodyColour];
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15789603

复制
相关文章

相似问题

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