我正在尝试在动画期间更改UIImage饱和度。但这样做似乎太沉重/太慢了:
-(void) changeSaturation:(CGFloat)value{
CIContext * context = [CIContext contextWithOptions:nil];
CIFilter *colorControlsFilter = [CIFilter filterWithName:@"CIColorControls"];
CIImage *image = self.imageView.image.CIImage;
[colorControlsFilter setValue:image forKey:@"inputImage"];
[colorControlsFilter setValue:[NSNumber numberWithFloat:value] forKey:@"inputSaturation"];
CIImage *outputImage = [colorControlsFilter outputImage];
CGImageRef cgimg = [context createCGImage:outputImage
fromRect:[outputImage extent]];
UIImage *newImage = [UIImage imageWithCGImage:cgimg];
self.imageView.image = newImage;
CGImageRelease(cgimg);}
每次用户拖动视图时都会调用此方法(当距离发生变化时,饱和度也会发生变化),这显然不是正确的方法,但我希望有类似的行为。 我想知道我是否可以通过UIImageview上的图层来实现这一目标。
有人可以告诉我如何实现我的目标吗?
答案 0 :(得分:1)
1)以异步方式计算过滤后的图像:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
UIImage * filtredImage = /*Your method*/
dispatch_async(dispatch_get_main_queue(), ^{
self.imageView.image = filtredImage;
}
}
2)平滑变化图像:使用coreAnimation。 self.layer - 在您的视图中是顶级CALayer
- (UIImage*)changeSaturation:(CGFloat)value
{
//Your filter process
}
- (void)changeSaturation:(CGFloat)value duration:(CGFloat)duration
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
NSUInteger cadreCount = ceil(duration * 60);
CGFloat incrimentValue = value / cadreCount;
NSMutableArray * mutArrayAnimationValues = [NSMutableArray new];
for (int cadreIndex = 0; cadreIndex <= cadreCount; cadreIndex++)
{
CGFloat currentValue = incrimentValue * cadreIndex;
UIImage * imageForCurrentCadr = [self changeSaturation:currentValue];
[mutArrayAnimationValues addObject:(id)imageForCurrentCadr.CGImage];
}
dispatch_async(dispatch_get_main_queue(), ^{
self.layer.contents = (id)[mutArrayAnimationValues lastObject];
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"contents"];
[animation setValues:mutArrayAnimationValues];
[animation setDuration:duration];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
[animation setFillMode:kCAFillModeBoth];
[pathAnimation setRemovedOnCompletion:NO];
[self.layer addAnimation:pathAnimation forKey:@"imageAnimation"];
}
}
}
答案 1 :(得分:1)
原来我的主要问题是被拖动的视图包含一个大分辨率的图像。我的解决方案完全正常。
答案 2 :(得分:0)
如果您在0.0和1.0之间更改饱和度,则可以在正常图像上方添加黑白(灰度)图像并更改其alpha值。它将是最高效的。