objective c - iOS - Setting blurred image on top of other views, odd issues -
so, i've got odd scenario.
in ios app, i'm trying blur content area of screen when popover opened. have working when using core image, when using gaussian blur- none of other blurs work, odd.
i tried doing same gpuimage, , blurs far faster, doesn't put view on top of other views!
to summarize: in source below, setbluronview work properly- setbluronviewwithgpuimage appears not working. blur view (tag 6110) created, app doesn't blur.
note: on ios 6, in simulator.
here's relevant source:
// screenblur.m #import <quartzcore/quartzcore.h> #import <coreimage/coreimage.h> #import <gpuimage/gpuimage.h> #import "screenblur.h" #import "globaldata.h" #import "logger.h" @implementation screenblur + (void) setbluronviewwithgpuimage:(uiview*)view { gpuimagepicture *imagesource = [[gpuimagepicture alloc] initwithimage:[self capturescreeninrect:view.frame inview:view]]; gpuimagegaussianblurfilter *blur = [[gpuimagegaussianblurfilter alloc] init]; [imagesource addtarget:blur]; [imagesource processimage]; [self setimage:[imagesource imagefromcurrentlyprocessedoutput] toview:view]; } + (void) setbluronview:(uiview *)view { //http://stackoverflow.com/questions/17041669/creating-a-blurring-overlay-view ciimage *inputimage = [ciimage imagewithcgimage:[self capturescreeninrect:view.frame inview:view].cgimage]; //cicontext *context = [cicontext contextwithoptions:nil]; if ([globaldata getinstance].cicontext == nil) { [logger log:@"cicontext not exist, creating..." fromclass:@"screenblur"]; // [globaldata getinstance].cicontext = [cicontext contextwithoptions:nil]; //cpu context [globaldata getinstance].cicontext = [cicontext contextwitheaglcontext:[[eaglcontext alloc] initwithapi:keaglrenderingapiopengles2]]; } //set blur filter cifilter *filter = [cifilter filterwithname:@"cigaussianblur"]; [filter setvalue:inputimage forkey:kciinputimagekey]; [filter setvalue:[nsnumber numberwithfloat:3.0f] forkey:@"inputradius"]; ciimage *result = [filter valueforkey:kcioutputimagekey]; // cigaussianblur has tendency shrink image little, // ensures matches bounds of our original image cgimageref cgimage = [[globaldata getinstance].cicontext createcgimage:result fromrect:[inputimage extent]]; [self setimage:[uiimage imagewithcgimage:cgimage] toview:view]; } + (void) setimage:(uiimage*)blurredimage toview:(uiview*)view { uiview *blurview = [[uiview alloc] initwithframe:cgrectmake(0, 0, blurredimage.size.width, blurredimage.size.height)]; [blurview setbackgroundcolor:[uicolor colorwithpatternimage:blurredimage]]; [blurview settag:6110]; //set image foreground view [view addsubview:blurview]; [view bringsubviewtofront:blurview]; } //same method above, resizes screenshot before applying blur increased performance @ expense of image quality. + (void) setbluronviewperformancemode:(uiview *)view { //http://stackoverflow.com/questions/17041669/creating-a-blurring-overlay-view uiimage *screenshot = [self imagewithimage:[self capturescreeninrect:view.frame inview:view] scaledtosize:cgsizemake(view.frame.size.width / 2, view.frame.size.height / 2)]; ciimage *inputimage = [ciimage imagewithcgimage:screenshot.cgimage]; //cicontext *context = [cicontext contextwithoptions:nil]; if ([globaldata getinstance].cicontext == nil) { [logger log:@"cicontext not exist, creating..." fromclass:@"screenblur"]; // [globaldata getinstance].cicontext = [cicontext contextwithoptions:nil]; //cpu context [globaldata getinstance].cicontext = [cicontext contextwitheaglcontext:[[eaglcontext alloc] initwithapi:keaglrenderingapiopengles2]]; } //set blur filter cifilter *filter = [cifilter filterwithname:@"cigaussianblur"]; [filter setvalue:inputimage forkey:kciinputimagekey]; [filter setvalue:[nsnumber numberwithfloat:3.0f] forkey:@"inputradius"]; ciimage *result = [filter valueforkey:kcioutputimagekey]; //cgimageref cgimage = [[globaldata getinstance].cicontext createcgimage:result fromrect:[inputimage extent]]; cgimageref cgimage = [[globaldata getinstance].cicontext createcgimage:result fromrect:[inputimage extent]]; [self setimage:[self imagewithimage:[uiimage imagewithcgimage:cgimage] scaledtosize:view.frame.size] toview:view]; } + (uiimage *)imagewithimage:(uiimage *)image scaledtosize:(cgsize)newsize { //uigraphicsbeginimagecontext(newsize); uigraphicsbeginimagecontextwithoptions(newsize, no, 0.0); [image drawinrect:cgrectmake(0, 0, newsize.width, newsize.height)]; uiimage *newimage = uigraphicsgetimagefromcurrentimagecontext(); uigraphicsendimagecontext(); return newimage; } + (void) removeblurfromview:(uiview *)view { (uiview *subview in view.subviews) { if (subview.tag == 6110) { [subview removefromsuperview]; } } } + (uiimage *)capturescreeninrect:(cgrect)captureframe inview:(uiview*) view { calayer *layer; layer = view.layer; uigraphicsbeginimagecontext(view.bounds.size); cgcontextcliptorect (uigraphicsgetcurrentcontext(),captureframe); [layer renderincontext:uigraphicsgetcurrentcontext()]; uiimage *screenimage = uigraphicsgetimagefromcurrentimagecontext(); uigraphicsendimagecontext(); return screenimage; } @end
and in view controller, it's called with
[screenblur setbluronview:self.view];
i found workaround (or, knows, maybe how supposed done).
//screenblur.m + (gpuimageview*) getblurredimagewithgpuimagefromview:(uiview*)view { gpuimagepicture *imagesource = [[gpuimagepicture alloc] initwithimage:[self capturescreeninrect:view.frame inview:view] smoothlyscaleoutput:true]; gpuimagefastblurfilter *blur = [[gpuimagefastblurfilter alloc] init]; [blur setblurpasses:3]; [imagesource addtarget:blur]; gpuimageview *filteredview = [[gpuimageview alloc] initwithframe:view.frame]; [blur addtarget:filteredview]; [imagesource processimage]; return filteredview; } //viewcontroller.m //blur main screen gpuimageview *blurred = [screenblur getblurredimagewithgpuimagefromview:self.view]; [blurred settag:6110]; [self.view addsubview:blurred]; [self.view bringsubviewtofront:blurred];
Comments
Post a Comment