2

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!

Joe
  • 8,868
  • 8
  • 37
  • 59
Khushbu Desai
  • 1,003
  • 1
  • 10
  • 32
  • 1
    Have a look https://stackoverflow.com/questions/28906914/how-do-i-add-text-to-an-image-in-ios-swift – Sahil Aug 24 '17 at 06:15
  • Possible duplicate of [How to write text on image in Objective-C (iOS)?](https://stackoverflow.com/questions/6992830/how-to-write-text-on-image-in-objective-c-ios) – Yuyutsu Aug 24 '17 at 06:26
  • My suggestion is you should take Screenshot of portion ***(here image + label)*** that you want and save those image in gallery. [This is question/answer for how take screenshot of specific portion.](https://www.google.co.in/search?rlz=1C5CHFA_enIN748IN748&q=swift+3+take+screenshot+of+portion&spell=1&sa=X&ved=0ahUKEwjLs8ujnO_VAhVJLI8KHStUBmoQvwUIIigA&biw=1366&bih=566) – Govaadiyo Aug 24 '17 at 06:16
  • I cant use Screen Shot Because I am Creating PhotoEditing app So that It will be Editable. – Khushbu Desai Aug 24 '17 at 06:23

3 Answers3

1

You can't use addsubview there You need to draw it. Here is Sample code.

 UIGraphicsBeginImageContext(img1.size);

//draw image1

 [img1 drawInRect:CGRectMake(0, 0, img1.size.width, img1.size.height)];



 //draw label

[label drawTextInRect:CGRectMake((img1.size.width - label.frame.size.width)/2, (img1.size.height - label.frame.size.height)/2, label.frame.size.width, label.frame.size.height)];

//get the final image

UIImage *resultImage  = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();
Prashant Tukadiya
  • 15,838
  • 4
  • 62
  • 98
1

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();
Abdelahad Darwish
  • 5,969
  • 1
  • 17
  • 35
0

Try:

extension UIImageView {

    func makeImage() -> UIImage? {
        UIGraphicsBeginImageContextWithOptions(bounds.size, false, 0.0)

        guard let context = UIGraphicsGetCurrentContext() else { return nil }
        layer.render(in: context)
        let image = UIGraphicsGetImageFromCurrentImageContext()

        UIGraphicsEndImageContext()

        return image
    }

}

This basically takes a snapshot from your UIImageView.

krlbsk
  • 1,051
  • 1
  • 13
  • 24