0

I am using UIGraphicsBeginImageContextWithOptions with custom CGSize i don't know why i am getting stretched image?

UIGraphicsBeginImageContextWithOptions(CGSize(width: UIScreen.main.bounds.width, height: 200) , false, 0)
let cellRect = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 200)
view.drawHierarchy(in: cellRect, afterScreenUpdates: true)
let wholeImage = UIGraphicsGetImageFromCurrentImageContext()//stretched image
UIGraphicsEndImageContext()
Ramesh
  • 147
  • 1
  • 17
  • Can you attach the "stretched image" and are you seeing it stretched in the "Photos" app ? or somewhere else in your app? (seems like you are doing everything right code wise) – Unis Barakat Aug 30 '18 at 13:39

1 Answers1

1

You are setting image context to height of 200 but then you do the same for the cell rect, which means it will take whole screenshot and put it into (screenWidth, 200) frame. You need to keep the context of 200 and then just use view.bounds for cellRect and you should get the top 200 px of the view if that is what you need.

UIGraphicsBeginImageContextWithOptions(CGSize(width: UIScreen.main.bounds.width, height: 200) , false, 0)
let cellRect = self.view.bounds
self.view.drawHierarchy(in: cellRect, afterScreenUpdates: true)
let wholeImage = UIGraphicsGetImageFromCurrentImageContext()//stretched image
UIGraphicsEndImageContext()
Ladislav
  • 7,223
  • 5
  • 27
  • 31
  • Can you please help with https://stackoverflow.com/questions/51978273/how-to-remove-blur-view-for-particular-cell & https://stackoverflow.com/questions/51512727/no-results-label-on-empty-tableview ? – Ramesh Aug 31 '18 at 09:26