Guest User

Untitled

a guest
Jul 20th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.97 KB | None | 0 0
  1. func createImageForStory(completion: @escaping (_ result: UIImage?, _ error: String?) -> Void){
  2. let size = CGSize.init(width: 750, height: 1334)
  3. UIGraphicsBeginImageContextWithOptions(size, false, 0.0)
  4. if let context = UIGraphicsGetCurrentContext() {
  5. let rect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
  6. let image = UIImage(named: imagePath)
  7. image?.draw(in: rect)
  8. let username = "username" as NSString
  9. let usernameAttributes = [
  10. NSAttributedStringKey.font : UIFont.systemFont(ofSize: 38),
  11. NSAttributedStringKey.foregroundColor : UIColor.white
  12. ]
  13. let usernameSize = username.size(withAttributes: usernameAttributes)
  14. username.draw(
  15. in: CGRect(x:
  16. size.width/2-usernameSize.width/2,
  17. y: 796,
  18. width: usernameSize.width,
  19. height: usernameSize.height
  20. ),
  21. withAttributes: usernameAttributes
  22. )
  23. do {
  24. let imageData = try Data(contentsOf: photoURL)
  25. let profileImage = UIImage(data: imageData)
  26. let rectImage = CGRect(x: 278, y: 102, width: 196, height: 196)
  27. let bezierPath = UIBezierPath(arcCenter: CGPoint(x: rectImage.midX, y: rectImage.midY), radius: 98, startAngle: 0, endAngle: 2.0*CGFloat(Double.pi), clockwise: true)
  28. context.addPath(bezierPath.cgPath)
  29. context.clip()
  30. profileImage?.draw(in: rectImage)
  31. if let newImage = UIGraphicsGetImageFromCurrentImageContext() {
  32. UIGraphicsEndImageContext()
  33. completion(newImage, nil)
  34. }
  35. else {
  36. UIGraphicsEndImageContext()
  37. completion(nil, "Error".localize())
  38. }
  39. }
  40. catch {
  41. completion(nil, "Error".localize())
  42. }
  43. }
  44. else {
  45. completion(nil, "Error".localize())
  46. }
  47. UIGraphicsEndImageContext()
  48. }
Add Comment
Please, Sign In to add comment