2017-08-28 92 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

相關問題