我也有類似的任務,並使用此代碼:
func getResultImage() throws -> (image: UIImage, path: URL) {
UIGraphicsBeginImageContextWithOptions(bounds.size, false, UIScreen.main.scale)
defer {
UIGraphicsEndImageContext()
}
drawHierarchy(in: self.bounds, afterScreenUpdates: true)
guard let image = UIGraphicsGetImageFromCurrentImageContext() else {
throw ImageEditorViewError.noImageGenerated
}
guard let data = UIImagePNGRepresentation(image) else {
throw ImageEditorViewError.noDataRepresentation
}
let writePath = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("Image.png")
try data.write(to: writePath)
return (image, writePath)
}
您可以writePaht去除部分,因爲它是具體的我的任務
假設,這個代碼是你的容器視圖中和你有一些意見 - 貼在裏面
檢查此問題http://stackoverflow.com/questions/30696307/how-to-convert-a-uiview-to-a-image – iOS