ios - UIView --> UIImage for non-visible view? -
i'm trying go work around inability use custom ui elements in watchkit. app uses custom uibuttons (mroundedbutton) , i'm trying replicate them on watchkit interface creating images of them , using background wkinterfacebutton. problem i'm not able convert uiviews uiimage when view isn't visible. code i'm using, it's giving me index out of bounds error on drawviewhierarchyinrect
.
- (uiimage *)convertbuttontoimage:(mroundedbutton *)button { uigraphicsbeginimagecontextwithoptions(button.bounds.size, yes, 0); [button drawviewhierarchyinrect:button.bounds afterscreenupdates:yes]; uiimage *image = uigraphicsgetimagefromcurrentimagecontext(); uigraphicsendimagecontext(); return image; }
since there no screen/window in watchkit extension, how go creating images would-be views?
rather drawviewhierarchyinrect, try using renderincontext on layer.
i ran same problem describe. using drawviewhierarchinrect didn't work - black screen. after switching renderincontext, looks i'm able convert uiview uiimage app while it's in background (so without view being visible on screen), , can send on watch , it's displayed.
here's used:
+ (uiimage *)imagefromview:(uiview *)view { uigraphicsbeginimagecontextwithoptions(view.bounds.size, yes, 0.0); [view.layer renderincontext:uigraphicsgetcurrentcontext()]; // [view drawviewhierarchyinrect:view.bounds afterscreenupdates:no]; uiimage *capturedimage = uigraphicsgetimagefromcurrentimagecontext(); uigraphicsendimagecontext(); return capturedimage; }
then use openparentapplication watchkit extension ping phone, convert uiimage nsdata in order pass in handlewatchkitextensionrequest. looks it's working me far, too. done on simulator, btw.
Comments
Post a Comment