Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- func createImageForStory(completion: @escaping (_ result: UIImage?, _ error: String?) -> Void){
- let size = CGSize.init(width: 750, height: 1334)
- UIGraphicsBeginImageContextWithOptions(size, false, 0.0)
- if let context = UIGraphicsGetCurrentContext() {
- let rect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
- let image = UIImage(named: imagePath)
- image?.draw(in: rect)
- let username = "username" as NSString
- let usernameAttributes = [
- NSAttributedStringKey.font : UIFont.systemFont(ofSize: 38),
- NSAttributedStringKey.foregroundColor : UIColor.white
- ]
- let usernameSize = username.size(withAttributes: usernameAttributes)
- username.draw(
- in: CGRect(x:
- size.width/2-usernameSize.width/2,
- y: 796,
- width: usernameSize.width,
- height: usernameSize.height
- ),
- withAttributes: usernameAttributes
- )
- do {
- let imageData = try Data(contentsOf: photoURL)
- let profileImage = UIImage(data: imageData)
- let rectImage = CGRect(x: 278, y: 102, width: 196, height: 196)
- let bezierPath = UIBezierPath(arcCenter: CGPoint(x: rectImage.midX, y: rectImage.midY), radius: 98, startAngle: 0, endAngle: 2.0*CGFloat(Double.pi), clockwise: true)
- context.addPath(bezierPath.cgPath)
- context.clip()
- profileImage?.draw(in: rectImage)
- if let newImage = UIGraphicsGetImageFromCurrentImageContext() {
- UIGraphicsEndImageContext()
- completion(newImage, nil)
- }
- else {
- UIGraphicsEndImageContext()
- completion(nil, "Error".localize())
- }
- }
- catch {
- completion(nil, "Error".localize())
- }
- }
- else {
- completion(nil, "Error".localize())
- }
- UIGraphicsEndImageContext()
- }
Add Comment
Please, Sign In to add comment