Search code examples
iosswiftswift3photo-galleryimage-editing

UILabel on UIImage and save that image in gallery in Swift 3 for Photo Editing App


I want to add Label on UIImage, for this I have used One Label And adding this on Image Using imgImage.addSubview(txtLabel).Now I want to Save That Image in Galley. For Saving the Image I have Used below method.

    @IBAction func btnSave(_ sender: AnyObject) {
    UIImageWriteToSavedPhotosAlbum(imgImage.image!, self, #selector(image(_:didFinishSavingWithError:contextInfo:)), nil)
    }

But It will not saving image with Label. How can I achieve it.

Please Help

Thanks!


Solution

  • you can render image layer with text

    like this // imageWithLabel handle final image

    txtLabel.backgroundColor = UIColor.clearColor()
    txtLabel.textAlignment = .Center
    txtLabel.textColor = UIColor.whiteColor()
    txtLabel.text = text
    
    UIGraphicsBeginImageContextWithOptions(txtLabel.bounds.size, false, 0);
    imgImage.layer.renderInContext(UIGraphicsGetCurrentContext()!)
    label.layer.renderInContext(UIGraphicsGetCurrentContext()!)
    let imageWithLabel = UIGraphicsGetImageFromCurrentImageContext() // here is final image
    UIGraphicsEndImageContext();