ios - Add blur in UICollectionViewCell -
i'm trying snapshot of uicollectionviewcell, blur , add subview uicollectionviewcell.
some weird problems happening.
mycollectionviewcontroller - (uicollectionviewcell *)collectionview:(uicollectionview *)collectionview cellforitematindexpath:(nsindexpath *)indexpath { uicollectionviewcell<mycollectionviewcellprotocol> *cell = [collectionview dequeuereusablecellwithreuseidentifier:@"mycollectionviewcell" forindexpath:indexpath]; cell.modelvariable = self.modelsarray[indexpath.item]; return cell; } @interface mycollectionviewcell : uicollectionviewcell <mycollectionviewcellprotocol> @property (strong, nonatomic) promo *modelvariable; @end #import "uiimage+imageeffects.h" // wwdc 2013 #import "uiview+snapshot.h" // category create snapshot uiview @implementation promorowcollectionviewcell - (void)setmodelvariable:(modelvariableclass *)modelvariable { _modelvariable = modelvariable; // settings other stuff importante cell view self.mainimageview.image = modelvariable.mainimage; self.titlelabel.text = modelvariable.title; self.shortdescriptionlabel.text = modelvariable.detaileddescription; // removing snapshot before taking other snapshot // or in case cell reused , modelvariable not in blurstate [self.blurimageview removefromsuperview]; self.blurimageview = nil; [self.myview removefromsuperview]; self.myview = nil; if (modelvariable.state == blurstate) { // attempt 1 // uigraphicsbeginimagecontext(self.bounds.size); // [self drawviewhierarchyinrect:self.bounds afterscreenupdates:yes]; // uiimage *image = uigraphicsgetimagefromcurrentimagecontext(); // uigraphicsendimagecontext(); // attempt 2 // uigraphicsbeginimagecontextwithoptions(self.bounds.size, self.opaque, 0.0); // [self.layer renderincontext:uigraphicsgetcurrentcontext()]; // uiimage *imgage = uigraphicsgetimagefromcurrentimagecontext(); // uigraphicsendimagecontext(); // test showed weird behavior // uiview *snapshotview = [self snapshotviewafterscreenupdates:yes]; // self.myview = snapshotview; // snapshotview.backgroundcolor = [uicolor colorwithred:1 green:0 blue:0 alpha:0.2]; // cgrect frame = snapshotview.frame; // frame.origin = cgpointmake(20, 20); // snapshotview.frame = frame; // [self addsubview:snapshotview]; // attempt 3 // red background , shifting frame can see red rectangle , not snapshot uiview *snapshotview = [self snapshotviewafterscreenupdates:yes]; snapshotview.backgroundcolor = [uicolor colorwithred:1 green:0 blue:0 alpha:0.2]; // see things better // self.blockedpromoimageview = [[uiimageview alloc] initwithimage:image]; // attempts 1 , 2 self.blurimageview = [[uiimageview alloc] initwithimage:snapshotview.snapshotimage.applylighteffect]; // self.blurimageview.frame = self.bounds; self.blockedpromoimageview.frame = cgrectmake(20, 20, self.bounds.size.width, self.bounds.size.height); // see things better // self.blockedpromoimageview.backgroundcolor = [uicolor colorwithred:1 green:0 blue:0 alpha:0.2]; // attempts 1 , 2 [self addsubview:self.blurimageview]; } } @end uiview+snapshot - (uiimage *)snapshotimage { uigraphicsbeginimagecontextwithoptions(self.bounds.size, no, 0.0); [self.layer renderincontext:uigraphicsgetcurrentcontext()]; uiimage *resultingimage = uigraphicsgetimagefromcurrentimagecontext(); uigraphicsendimagecontext(); return resultingimage; }
attempt 1:
attempt 2:
test weird behavior:
attempt 3:
so can see, attempts 1, 2 , 3 seems didn't take snapshot. i'd expect red rectangle have snapshot of view in back.
as weird behavior test, i'd expect can seen in image, except red rectangle @ front.
does knows whats going on? or how solve it?
thank you.
edit:
i created simple project minimum amount of code reproduce problem. https://www.dropbox.com/sh/j1mhxj367kzn81y/aadw8ds9nvlmyjudbn08mojxa?dl=0
you don't need use afterscreenupdates flag if want image , text change come through in screenshot.
you can edit image , text, remove views , save cell image in standard way:
-(uiimage*) takesnapshot { uigraphicsbeginimagecontext(self.frame.size); [self.layer renderincontext: uigraphicsgetcurrentcontext()]; uiimage *image = uigraphicsgetimagefromcurrentimagecontext(); uigraphicsendimagecontext(); return image; }
then add blur , add view.
if (modelvariable.state == blurstate) { uiimage *snapshot = [self takesnapshot]; uiimage *blurredimage = [snapshot applylighteffect]; self.blurredimageview = [[uiimageview alloc] initwithimage:blurredimage]; [self.view addsubview:self.blurredimageview]; }
Comments
Post a Comment