Danny182 Danny182 - 3 months ago 92
iOS Question

Convert Apple Emoji (String) to UIImage

I need all Apple Emojis.
I can get all the emojis and put them into a String by copying them from the site getemoji but in my app i need the emojis in the right order as images.


Is there a nice way to convert the emojis I copy into a String to a UIImage?

Or a better solution to get all the Apple emojis in the right order?

Answer

Add this extension to your project

extension String {
    func image() -> UIImage {
        let size = CGSize(width: 30, height: 35)
        UIGraphicsBeginImageContextWithOptions(size, false, 0);
        UIColor.whiteColor().set()
        let rect = CGRect(origin: CGPointZero, size: size)
        UIRectFill(CGRect(origin: CGPointZero, size: size))
        (self as NSString).drawInRect(rect, withAttributes: [NSFontAttributeName: UIFont.systemFontOfSize(30)])
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image
    }

}

The code above draws the current String to an Image Context with a white background color and finally transform it into a UIImage.

Now you can write

enter image description here

Comments