ios - overlaying text on image using swift -
i trying overlay text on image using swift , looking @ code here: (src: how add text image in ios swift)
this places text right in center. have been changing values in
var rect = cgrectmake(10,150, inimage.size.width, inimage.size.height)
but unable text show in lower left corner. can , show missing here ?
i adding modified image using line:
modimage = self.texttoimage("000", inimage: uiimage(named:"thisimage.png")!, atpoint: cgpointmake(10, 400))
below function...
func texttoimage(drawtext: nsstring, inimage: uiimage, atpoint: cgpoint) -> uiimage{ // setup font specific variables var textcolor = uicolor.whitecolor() var textfont = uifont(name: "helvetica bold", size: 12)! // setup image context using passed image let scale = uiscreen.mainscreen().scale uigraphicsbeginimagecontextwithoptions(inimage.size, false, scale) // setup font attributes later used dictate how text should drawn let textfontattributes = [ nsfontattributename: textfont, nsforegroundcolorattributename: textcolor, ] // put image rectangle large original image inimage.drawinrect(cgrectmake(0, 0, inimage.size.width, inimage.size.height)) // create point within space bit image var rect = cgrectmake(atpoint.x, atpoint.y, inimage.size.width, inimage.size.height) // draw text image drawtext.drawinrect(rect, withattributes: textfontattributes) // create new image out of images have created var newimage = uigraphicsgetimagefromcurrentimagecontext() // end context have image need uigraphicsendimagecontext() //pass image caller return newimage }
details
xcode 8.2.1, swift 3
code
viewcontroller.swift
class viewcontroller: uiviewcontroller { let imageview = uiimageview(frame: cgrect(x: 50, y: 50, width: 300, height: 300)) override func viewdidload() { super.viewdidload() // additional setup after loading view, typically nib. let image = uiimage(named: "wall")!.addtext("swift", atpoint: cgpoint(x: 20, y: 320), textcolor:nil, textfont:uifont.systemfont(ofsize: 40)) imageview.image = image view.addsubview(imageview) } }
uiimage.swift
import uikit extension uiimage { func addtext(_ drawtext: nsstring, atpoint: cgpoint, textcolor: uicolor?, textfont: uifont?) -> uiimage { // setup font specific variables var _textcolor: uicolor if textcolor == nil { _textcolor = uicolor.white } else { _textcolor = textcolor! } var _textfont: uifont if textfont == nil { _textfont = uifont.systemfont(ofsize: 16) } else { _textfont = textfont! } // setup image context using passed image uigraphicsbeginimagecontext(size) // setup font attributes later used dictate how text should drawn let textfontattributes = [ nsfontattributename: _textfont, nsforegroundcolorattributename: _textcolor, ] [string : any] // put image rectangle large original image draw(in: cgrect(x: 0, y: 0, width: size.width, height: size.height)) // create point within space bit image let rect = cgrect(x: atpoint.x, y: atpoint.y, width: size.width, height: size.height) // draw text image drawtext.draw(in: rect, withattributes: textfontattributes) // create new image out of images have created let newimage = uigraphicsgetimagefromcurrentimagecontext() // end context have image need uigraphicsendimagecontext() //pass image caller return newimage! } }
result
about memory leaks (use extension in loop)
// test memory leaks dispatchqueue.global(qos: .background).async { [weak self] in index in 0...2000 { print(index) autoreleasepool { () -> () in let image = self?.imageview.image!.addtext("swift", atpoint: cgpoint(x: 20, y: 320), textcolor:nil, textfont:uifont.systemfont(ofsize: 40)) self?.imageview.image = image } } }
Comments
Post a Comment