Providing Emoji (String) for a high-resolution image in iOS using the / UIgraphicscontext extension

So, it is known that apple images that support Emoji in unicode can be extracted with a resolution of up to 160x160 pixels. There is an available tool that can do this on OS X by pulling data from the "apple color emoji" font here: https://github.com/tmm1/emoji-extractor

I am interested in extracting / using Apple Emoji images in a similar way in iOS, I have found the following solution and really love the implementation here: https://stackoverflow.com/a/316618/

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 } } 

However, there is a problem that I am encountering with this implementation. If I use the line extension above, it is obvious to me that the resulting image does not have a resolution of 160x160 px. I mean, I cannot create an image of comparable quality, say, if I included a 160x160 image in my project assets.

What is the reason for this, and can I solve this problem without importing more than 1700 emoji images into my project? I believe that the font on iOS should still contain the data needed to create 160x160 image samples. The String extension is ideal because it allows me to reference emoji characters more freely than if I just imported all these images.

I believe this might require some low-level code, which may be more complex than it costs, but I would really like to know more about the root of this problem and possible fixes. I am sure that this is due to the fact that the apple does not give emoji at a level higher than necessary, but, of course, there is a way to redefine this. I also know that there are other high resolution images of Emoji, this question is not about that.

edit: inappropriate permissions

edit 10/18/16: I came to the conclusion that the problem may be due to the fact that the original image that was selected does not have the highest resolution that is considered possible. Emoji does not seem to display at resolutions higher than 48x48 px, when it was demonstrated that emoji images can be displayed up to 160x160 px on OS X and can be torn from "Apple Color Emoji.ttf / .ttc" at 160x160 px. I'm not sure if there is even a possible solution to this problem, since it seems like a hard limit imposed on how iOS uses Unicode character codes that map to png image data.

+5
source share
2 answers

Your problem is that you take a string of a certain font size and then you paste it into an area of ​​another specific size. These two may not match, causing the scalable image to scale to fit.

Either calculate the size required for the given line and font, or calculate the necessary font size to allow the given line to fit into the selected size.

+1
source

Replace

 UIGraphicsBeginImageContext(rect.size) 

or

 UIGraphicsBeginImageContextWithOptions(rect.size, false, 0.0) 

with

 UIGraphicsBeginImageContextWithOptions(rect.size, false, 3.0) 

Scale 2.0 also works. I see no difference between this and 3.0 with a quick look.

0
source

Source: https://habr.com/ru/post/1258350/


All Articles