ios - How to insert labels on top of images saved to .WriteToFile in PNG's Swift -
i'm trying save label on top of image using .writetofile .
here's code i'm using save image :
let selectedimage: uiimage = image.image! let filemanager = nsfilemanager.defaultmanager() let paths = nssearchpathfordirectoriesindomains(.documentdirectory, .userdomainmask, true)[0] string let filepathtowrite = "\(paths)/user_profile_image.png" let imagedata: nsdata = uiimagepngrepresentation(selectedimage)! let jpgimagedata = uiimagejpegrepresentation(selectedimage, 1.0) filemanager.createfileatpath(filepathtowrite, contents: jpgimagedata, attributes: nil) // check file saved let getimagepath = (paths nsstring).stringbyappendingpathcomponent("user_profile_image.png") if (filemanager.fileexistsatpath(getimagepath)) { print("file available"); } else { print("file not available"); }
i'm trying retrieve :
if let pdfurl = nsbundle.mainbundle().urlforresource("user_profile_image", withextension: "png", subdirectory: nil, localization: nil),data = nsdata(contentsofurl: pdfurl), baseurl = pdfurl.urlbydeletinglastpathcomponent { let webview = uiwebview(frame: cgrectmake(20,20,self.view.frame.size.width-40,self.view.frame.size.height-40)) print("hello world") webview.loaddata(data, mimetype: "application/pdf", textencodingname:"", baseurl: baseurl)//application/ self.view.addsubview(webview) }
but isn't working. ideas?
in current approach, placing uilabel on top of image, not modify image in anyway, label sits on it. write text on image need use graphics context draw text on image.
have @ below code, take image , write hello world in white text in bottom right corner.
class viewcontroller: uiviewcontroller { @iboutlet weak var imageview: uiimageview! override func viewdidload() { super.viewdidload() // additional setup after loading view, typically nib. } override func viewdidappear(animated: bool) { super.viewdidappear(animated) let font = uifont.boldsystemfontofsize(30) let showtext:nsstring = "hello world" // setting attr: font name, color...etc. let attr = [nsfontattributename: font, nsforegroundcolorattributename:uicolor.whitecolor()] // getting size let sizeoftext = showtext.sizewithattributes(attr) let image = uiimage(named: "dog") let rect = cgrectmake(0, 0, image!.size.width, image!.size.height) uigraphicsbeginimagecontextwithoptions(cgsize(width: rect.size.width, height: rect.size.height), true, 0) // drawing our image graphics context image?.drawinrect(rect) // drawing text showtext.drawinrect(cgrectmake(rect.size.width-sizeoftext.width-10, rect.size.height-sizeoftext.height-10, rect.size.width, rect.size.height), withattributes: attr) // getting image let newimage = uigraphicsgetimagefromcurrentimagecontext(); uigraphicsendimagecontext() self.imageview.image = newimage } }
once have done this, can save newimage file, or overwrite existing one.
i used bottom of guide reference, it's while since done this
Comments
Post a Comment