2017-08-28 91 views
0

我的问题是我怎样才能保存与相册中的textview图像? 我的意思是,当我在图像的仪式,我想将图像保存与TextView的我把它写 Click to see the Image我可以使用textview在相册中保存图像吗?

,这里是我的代码,但不工作

@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) 

} 

它的工作,但文本没有按” t保存 enter image description here

+0

什么意思“不工作”?你能提供任何错误日志或描述更好的行为吗? – araknoid

+0

@araknoid我的意思是在专辑 – faten

+0

不保存在光电保存,但没有文字的看法?或者根本没有被保存的东西? – LoganHenderson

回答

3

您必须使textView成为UIImageView的子视图。然而这在故事板中是不可能的。完成你想要什么,最简单的方法是使用一个UIView作为一个容器视图,添加图像视图和TextView的为子视图,那么你可以使用下面的功能快照UIView的。你的故事板文档大纲应该是这样的

enter image description here

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 
    } 

所以您应将IBAction为更改为以下。您必须获取包含这两个元素的UIView的引用。我在下面的代码中将它称为“containerView”。

@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) 

} 
+0

它的工作,但文本不保存它保存的TextView的背景,但文字不救,我想,当用户把一些文字,并点击保存按钮中的所有文本,并保存图像,我希望你能理解我 – faten

+0

我不不明白。也许,如果你添加的照片,你的问题,以显示你的意思 – LoganHenderson

+0

[链接](http://g.recordit.co/uWcHuv8MZX.gif)文本中不保存 – faten

相关问题