Reputation: 25
My question is how can i save Image with textview in album ? i mean when i rite on image i want to save the image with the textview i write it Click to see the Image
and here is my code but doesn't work
@IBAction func SaveImage(_ sender: Any) {
let imageRepresentation = UIImagePNGRepresentation(cv1image.image!)
let imageData = UIImage(data: imageRepresentation!)
UIImageWriteToSavedPhotosAlbum(imageData!, nil, nil, nil)
let alert = UIAlertController(title: "Completed", message: "Image has been saved!", preferredStyle: .alert)
let action = UIAlertAction(title: "Ok", style: .default, handler: nil)
alert.addAction(action)
self.present(alert, animated: true, completion: nil)
}
it's work but the text doesn't saved enter image description here
Upvotes: 0
Views: 309
Reputation: 1442
You must make the textView a subview of the UIImageView. However this is not possible to do in storyboards. The simplest way to accomplish what you want is to use a UIView as a container view, add the image view and textview as subviews, then you can snapshot the UIView using the function below. Your storyboard document outline should look like this
func snapshot(view: UIView) -> UIImage? {
UIGraphicsBeginImageContext(view.bounds.size)
let savedFrame = view.frame;
view.layer.render(in: UIGraphicsGetCurrentContext()!)
let image = UIGraphicsGetImageFromCurrentImageContext();
view.frame = savedFrame;
UIGraphicsEndImageContext();
return image
}
So you should change your IBAction to the following. You must grab a reference to the UIView that contains both elements. I called it "containerView" in the below code.
@IBAction func SaveImage(_ sender: Any) {
let image = snapshot(view: containerView)
let imageRepresentation = UIImagePNGRepresentation(image!)
let imageData = UIImage(data: imageRepresentation!)
UIImageWriteToSavedPhotosAlbum(imageData!, nil, nil, nil)
let alert = UIAlertController(title: "Completed", message: "Image has been saved!", preferredStyle: .alert)
let action = UIAlertAction(title: "Ok", style: .default, handler: nil)
alert.addAction(action)
self.present(alert, animated: true, completion: nil)
}
Upvotes: 4