您的位置:首页 > 产品设计 > UI/UE

如何正确选择uiimage之间不同的过滤器(gpuimage)

2015-10-30 11:21 381 查看
from: http://www.4byte.cn/question/16668/how-to-correctly-alternate-between-different-filters-of-uiimage-gpuimage.html
I am working on a filtering application, similar to the GPUImage "filterShowCase" project, but am focusing on filtering static images (selected from the user's library). The main view displays the image selected by the imagepicker as follows:

sourcePicture = [[GPUImagePicture alloc] initWithImage:sourceImage smoothlyScaleOutput:YES];
filter = [[GPUImageSepiaFilter alloc] init];

filterView = [[GPUImageView alloc]initWithFrame:self.view.frame];
filterView.contentMode = UIViewContentModeScaleAspectFit;
filterView.clipsToBounds = YES;

//Setup targets
[sourcePicture addTarget:filter];
[filter addTarget:filterView];
[sourcePicture processImage];

[self.view addSubview:filterView];


This all works, and the image is filtered in sepia. I am allowing a user to change filters based on user input, in aim for quick alternation between different filters - this is all done in a switch statement...
switch (filterNumber)
{
case GPUIMAGE_SEPIA:
{
self.title = @"Sepia Tone";

filter = [[GPUImageSepiaFilter alloc] init];
}; break;
//a bunch of other filters...

}
[sourcePicture removeAllTargets];
[sourcePicture addTarget:filter];
[filter addTarget:filterView];
[sourcePicture processImage];


This (^) is the current process I am using, but there is a small time interval between the filter type selection and the actual modification of the image to match that filter.

I previously tried just doing
[sourcePicture
processImage]
but that didn't work (didn't change the image in the GPUImageView), so what am i doing something wrong? - or is the intended performance of the system?

Thanks!

ANSWER Look at Brad Larsons' comment to this question.
我工作在一个过滤应用程序,类似于gpuimage”filtershowcase”项目,但我以过滤的静态图像(选择从用户的图书馆)。主视图中显示选定的imagepicker作为图像如下:
sourcePicture = [[GPUImagePicture alloc] initWithImage:sourceImage smoothlyScaleOutput:YES];
filter = [[GPUImageSepiaFilter alloc] init];

filterView = [[GPUImageView alloc]initWithFrame:self.view.frame];
filterView.contentMode = UIViewContentModeScaleAspectFit;
filterView.clipsToBounds = YES;

//Setup targets
[sourcePicture addTarget:filter];
[filter addTarget:filterView];
[sourcePicture processImage];

[self.view addSubview:filterView];


这所有的作品,并对图像进行滤波,深褐色的。我允许用户改变基于用户输入滤波器,在目标的不同的过滤器之间的快速交替,这是在switch语句中的一切…
switch (filterNumber)
{
case GPUIMAGE_SEPIA:
{
self.title = @"Sepia Tone";

filter = [[GPUImageSepiaFilter alloc] init];
}; break;
//a bunch of other filters...

}
[sourcePicture removeAllTargets];
[sourcePicture addTarget:filter];
[filter addTarget:filterView];
[sourcePicture processImage];


这(^)是当前进程中我使用,但有过滤器的选型和图像的实际改进匹配滤波器之间的一个小的时间间隔。

我以前试过只是做
[sourcePicture
processImage]
但没有工作(并没有改变在gpuimageview图像),所以我做了什么错

事?或是系统预期的性能?

谢谢!

答案看看布拉德拉松斯评论这个问题。

最佳答案 (Best Answer)

As Brad Larson wrote, using the [filter forceProcessingAtSize] will reduce the execution time of the filters.

比如拉尔森写道,使用[过滤forceprocessingatsize ]将减少过滤器的执行时间。

答案 (Answer) 2

I have an app that sort of does the same, and what I do is that I keep my baseImage at hand, and once I am done applying the filter and showing the image with filter I reset everything. The moment a user selects a different filter I use the baseImage again
to create the new filtered image and show it.

So in this example you could for example move the [sourcePicture removeAllTargets]; to right after this [self.view addSubview:filterView];

That saves you work when you want to apply a new filter. Also, there is a quicker (dirtier way to filter a picture when you are working with an image that already exists. I got this from the documentation that came with GPUImage and it works like a charm.
GPUImageSketchFilter *stillImageFilter2 = [[GPUImageSketchFilter alloc] init];
tmp = [stillImageFilter2 imageByFilteringImage:[self baseImage]];
[self.imageView setImage: tmp];
tmp = nil;


Hope it helps you on your way.

我有一个应用程序,这样做同样的,和我所做的是,我把我的baseimage在手,又一次我做了应用过滤器和显示图像的滤波重置一切。当用户选择不同的滤波器,我用baseimage再次创造了新的图像滤波,显示它。

所以在这个例子中,例如你可以把[ sourcepicture removealltargets ];这[ self.view addsubview后右:filterview ];

保存你的时候你要申请一个新的过滤器的工作。同时,有一个更快的(脏的方式过滤的图片时,你是一个图像,已经存在的工作。我从来gpuimage文件和它的工作就像一个魅力。
GPUImageSketchFilter *stillImageFilter2 = [[GPUImageSketchFilter alloc] init];
tmp = [stillImageFilter2 imageByFilteringImage:[self baseImage]];
[self.imageView setImage: tmp];
tmp = nil;


希望它能帮助你对你的方式。

答案 (Answer) 3

Use this method for switch filtering , just pass your filter name string in Method
-(void)filterImage:(NSString *)aStrFilterOption {

CIImage *_inputImage = [CIImage imageWithCGImage:[imgOriginal CGImage]];
CIFilter *filter = [CIFilter filterWithName:aStrFilterOption];
[filter setValue:_inputImage forKey:kCIInputImageKey];

CGImageRef moi3 = [[CIContext contextWithOptions:nil]
createCGImage:filter.outputImage
fromRect:_inputImage.extent];
imgView.image = [UIImage imageWithCGImage:moi3];
CFRelease(moi3);
}


用这种方法开关滤波,只是通过你的过滤器中的名称字符串的方法
-(void)filterImage:(NSString *)aStrFilterOption {

CIImage *_inputImage = [CIImage imageWithCGImage:[imgOriginal CGImage]];
CIFilter *filter = [CIFilter filterWithName:aStrFilterOption];
[filter setValue:_inputImage forKey:kCIInputImageKey];

CGImageRef moi3 = [[CIContext contextWithOptions:nil]
createCGImage:filter.outputImage
fromRect:_inputImage.extent];
imgView.image = [UIImage imageWithCGImage:moi3];
CFRelease(moi3);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: