ios - A custom background for a modal VC and rotation -
i've implemented custom (blurred) background modal view controller on ipad. here's how it: in presented vc's viewwillshow:
take snapshot image of presenting vc's view, blur it, , add uiimage view on top of presenting vc. works well.
however, when rotate device while modal showing, blurred image gets out of sync view controller represents. image gets stretched fill screen, view controller doesn't resize views same way. when modal dismissed , blur goes away, transition looks bad.
i've tried taking snapshot in didrotatefrominterfaceorientation:
, , replacing blurred image, didn't work.
any advice on how solve this?
update: a sample project
my code (in modal view controller):
- (void)viewwillappear:(bool)animated { [super viewwillappear:animated]; [self drawbackgroundblurview]; } - (void)drawbackgroundblurview { if(_blurmodalbackground) { [_backgroundblurview removefromsuperview]; cgrect backgroundframe = self.presentingviewcontroller.view.bounds; _backgroundblurview = [[uiimageview alloc] initwithframe:backgroundframe]; _backgroundblurview.autoresizingmask = uiviewautoresizingflexiblewidth | uiviewautoresizingflexibleheight; [_backgroundblurview setimage:[stsutils imagebyblurringview:self.presentingviewcontroller.view]]; [self.presentingviewcontroller.view addsubview:_backgroundblurview]; } }
utils code
+ (uiimage *)imagebyblurringview:(uiview *)view { uiwindow *window = [[[uiapplication sharedapplication] windows] lastobject]; bool isportrait = (uiinterfaceorientationisportrait([[uiapplication sharedapplication] statusbarorientation])); cgrect frame; if(isportrait) { frame = cgrectmake(view.frame.origin.x, view.frame.origin.y, view.frame.size.height, view.frame.size.width); } else { frame = cgrectmake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, view.frame.size.height); } uigraphicsbeginimagecontextwithoptions(frame.size, no, window.screen.scale); [view drawviewhierarchyinrect:frame afterscreenupdates:no]; // snapshot uiimage *snapshotimage = uigraphicsgetimagefromcurrentimagecontext(); // apply blur uicolor *tintcolor = [uicolor colorwithwhite:1.0 alpha:0.3]; uiimage *blurredsnapshotimage = [snapshotimage applyblurwithradius:8 tintcolor:tintcolor saturationdeltafactor:1.8 maskimage:nil]; uigraphicsendimagecontext(); return blurredsnapshotimage; }
i coded simple example takes screenshot presentingviewcontroller
, sets background image. here implementaion of modal view controller. , works (both on viewwillappear
, handles rotation properly). please post stsutils
code?
#import "screenshoterviewcontroller.h" @interface screenshoterviewcontroller () @property (weak, nonatomic) iboutlet uiimageview *imageview; - (ibaction)buttonbackpressed; @end @implementation screenshoterviewcontroller - (void)viewwillappear:(bool)animated { [super viewwillappear:animated]; [self updateimage]; } - (void)updateimage { uigraphicsbeginimagecontext(self.view.bounds.size); cgcontextref context = uigraphicsgetcurrentcontext(); [self.presentingviewcontroller.view.layer renderincontext:context]; uiimage *img = uigraphicsgetimagefromcurrentimagecontext(); uigraphicsendimagecontext(); self.imageview.image = img; } - (void)didrotatefrominterfaceorientation:(uiinterfaceorientation)frominterfaceorientation { [self updateimage]; } - (ibaction)buttonbackpressed { [self dismissviewcontrolleranimated:yes completion:null]; } @end
update
in utils code replace this
[view drawviewhierarchyinrect:frame afterscreenupdates:no];
with this
[view drawviewhierarchyinrect:frame afterscreenupdates:yes];
from apple docs:
afterupdates boolean value indicates whether snapshot should rendered after recent changes have been incorporated. specify value no if want render snapshot in view hierarchy’s current state, might not include recent changes.
so taking screenshot did not included latest changes in view.
Comments
Post a Comment