Make images with name initials like Gmail in Swift programmatically for iOS

后端 未结 8 2228
孤城傲影
孤城傲影 2021-02-05 18:03

I need to display a profile pic of every user corresponding to his name in a UITableView. Until the image is downloaded I need to show an image with his name\'s first alphabet l

8条回答
  •  一个人的身影
    2021-02-05 18:33

    You can use https://github.com/bofiaza/IPImage, but you must to do some corrections in function generateImage().

    Change this code in source file:

    public func generateImage() -> UIImage? {
    
        let view = setupView()
    
        UIGraphicsBeginImageContextWithOptions(view.bounds.size, false, 0.0)
        view.drawHierarchy(in: view.bounds, afterScreenUpdates: true)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        print(image ?? "No image")
        return image
    }
    

    on this:

    public func generateImage() -> UIImage? {
    
        let view = setupView()
    
        UIGraphicsBeginImageContextWithOptions(view.bounds.size, false, 0.0)
        defer { UIGraphicsEndImageContext() }
        guard let currentContext = UIGraphicsGetCurrentContext() else {
            return nil
        }
        view.layer.render(in: currentContext)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        print(image ?? "No image")
        return image
    }
    

    It will be work correctly!

提交回复
热议问题