There are three imageViews in first view controller (two donuts png and one Simpson image). Are there any solutions that I can capture these images as one image and save it to photo library? Thanks.
Asked
Active
Viewed 448 times
0
-
Use an image graphics context. Draw the three images into it in back to front order. Done. – matt Aug 13 '18 at 00:33
1 Answers
2
Try this, passing in the base view that has all the subviews you need:
func createImage(from aView:UIView) -> UIImage {
UIGraphicsBeginImageContextWithOptions(CGSize(width: aView.frame.width, height: aView.frame.height), true, 0)
aView.layer.render(in: UIGraphicsGetCurrentContext()!)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image!
}
-
dfd Thx, I don't quite understand what parameter should I pass in your function. How am I suppose to get this UIview as a parameter in my case? THX again! – buzzmind Aug 13 '18 at 02:16
-
Your screenshot and question indicates that you have 3 image views, along with a "done" button. If you pass in the controller's root view you will create an image of everything including that "done" button. If you don't already have it set up, just put the three image views into an invisible "container" view and pass that into the function. – Aug 13 '18 at 13:06
-
dfd Thx, it works but the image is kinda low quality (like blurry) compare with drawing piece by piece in another image context. Do you know why? THX – buzzmind Aug 13 '18 at 16:41
-
dfd I change your code UIGraphicsBeginImageContextWithOptions(CGSize(width: aView.frame.width, height: aView.frame.height), true, 0) and it works well. THX – buzzmind Aug 13 '18 at 21:21