{"id":1116,"date":"2012-11-19T19:03:09","date_gmt":"2012-11-19T16:03:09","guid":{"rendered":"http:\/\/hxml.me\/blog\/?p=1116"},"modified":"2012-12-12T13:52:16","modified_gmt":"2012-12-12T10:52:16","slug":"cvetnoj-uiimage-iz-cherno-belogo-izobrazheniya","status":"publish","type":"post","link":"https:\/\/hxml.me\/blog\/news\/cvetnoj-uiimage-iz-cherno-belogo-izobrazheniya\/","title":{"rendered":"<!--:en-->Colored UIImage from grayscale image<!--:--><!--:ru-->\u0426\u0432\u0435\u0442\u043d\u043e\u0439 UIImage \u0438\u0437 \u0447\u0435\u0440\u043d\u043e-\u0431\u0435\u043b\u043e\u0433\u043e \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f<!--:-->"},"content":{"rendered":"<p><!--:en--><img loading=\"lazy\" decoding=\"async\" class=\"alignleft\" src=\"http:\/\/hxml.me\/img\/post\/tint_post.png\" alt=\"\" width=\"256\" height=\"192\" \/>For a very long time I couldn&#8217;t solve one problem that appeared time by time. Until it happened again a few day ago. But this time I decided that to put up with this state of affairs is not more possible and once again began to research. And the problem is the following. Assume you have a the monochrome image (for example, the icon of the stars for the Favorites button), and a skeleton application that implements the usage of skins (customization of the user interface). If one client wants the star to be red and another client wants it to be blue, the designer would have to repaint the image each time. But there must be an easier way to give a tint color using CoreGraphics functions.<!--:--><!--:ru--><img loading=\"lazy\" decoding=\"async\" class=\"alignleft\" src=\"http:\/\/hxml.me\/img\/post\/tint_post.png\" alt=\"\" width=\"256\" height=\"192\" \/>\u0414\u043e\u043b\u0433\u043e\u0435 \u0432\u0440\u0435\u043c\u044f \u043d\u0435 \u043c\u043e\u0433 \u043f\u043e\u0434\u0441\u0442\u0443\u043f\u0438\u0442\u044c\u0441\u044f \u043a \u043e\u0434\u043d\u043e\u0439 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0435, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0442\u043e \u0438 \u0434\u0435\u043b\u043e \u043f\u0435\u0440\u0438\u043e\u0434\u0438\u0447\u0435\u0441\u043a\u0438 \u0432\u0441\u043f\u043b\u044b\u0432\u0430\u043b\u0430. \u0412 \u043e\u0447\u0435\u0440\u0435\u0434\u043d\u043e\u0439 \u0440\u0430\u0437 \u044f \u0442\u0432\u0435\u0440\u0434\u043e \u0440\u0435\u0448\u0438\u043b, \u0447\u0442\u043e \u043c\u0438\u0440\u0438\u0442\u044c\u0441\u044f \u0441 \u0442\u0430\u043a\u0438\u043c \u043f\u043e\u043b\u043e\u0436\u0435\u043d\u0438\u0435\u043c \u0434\u0435\u043b \u043d\u0435 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0431\u043e\u043b\u0435\u0435 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u043c \u0438 \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u043d\u043e\u0439 \u0440\u0430\u0437 \u043f\u0440\u0438\u0441\u0442\u0443\u043f\u0438\u043b \u043a \u0438\u0441\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u043d\u0438\u044f\u043c. \u0410 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u0432 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c. \u041f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0438\u043c, \u0438\u043c\u0435\u0435\u0442\u0441\u044f \u0432 \u043d\u0430\u043b\u0438\u0447\u0438\u0435 \u043c\u043e\u043d\u043e\u0445\u0440\u043e\u043c\u043d\u0430\u044f \u043a\u0430\u0440\u0442\u0438\u043d\u043a\u0430 (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0438\u043a\u043e\u043d\u043a\u0430 \u0437\u0432\u0435\u0437\u0434\u044b \u0434\u043b\u044f \u043a\u043d\u043e\u043f\u043a\u0438 \u0418\u0437\u0431\u0440\u0430\u043d\u043d\u044b\u0435), \u0430 \u0442\u0430\u043a\u0436\u0435 \u043d\u0435\u043a\u0438\u0439 \u043a\u0430\u0440\u043a\u0430\u0441 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u0441\u043a\u0438\u043d\u043e\u0432 (\u043a\u0430\u0441\u0442\u043e\u043c\u0438\u0437\u0430\u0446\u0438\u044f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430). \u0415\u0441\u043b\u0438 \u043e\u0434\u0438\u043d \u043a\u043b\u0438\u0435\u043d\u0442 \u0445\u043e\u0447\u0435\u0442, \u0447\u0442\u043e\u0431\u044b \u0437\u0432\u0435\u0437\u0434\u0430 \u0431\u044b\u043b\u0430 \u043a\u0440\u0430\u0441\u043d\u043e\u0439, \u0430 \u0434\u0440\u0443\u0433\u043e\u0439, \u0447\u0442\u043e\u0431\u044b \u0441\u0438\u043d\u0435\u0439, \u0442\u043e \u0434\u0438\u0437\u0430\u0439\u043d\u0435\u0440\u0443 \u043f\u0440\u0438\u0434\u0435\u0442\u0441\u044f \u043a\u0430\u0436\u0434\u044b\u0439 \u0440\u0430\u0437 \u043f\u0435\u0440\u0435\u0434\u0435\u043b\u044b\u0432\u0430\u0442\u044c \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435. \u041d\u043e \u0437\u0430\u0447\u0435\u043c, \u043a\u043e\u0433\u0434\u0430 \u0434\u043e\u043b\u0436\u043d\u043e \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u043e\u0432\u0430\u0442\u044c \u0431\u043e\u043b\u0435\u0435 \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 \u043f\u0440\u0438\u0434\u0430\u0442\u044c \u043e\u0442\u0442\u0435\u043d\u043e\u043a \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044e \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0433\u0440\u0430\u0444\u0438\u0447\u0435\u0441\u043a\u0438\u0445 \u0444\u0443\u043d\u043a\u0446\u0438\u0439 CoreGraphics.<!--:--><!--more--><!--:en--><\/p>\n<p>My goal was to apply this approach to the component of UIButton, but it is not hard to realize, there is is no difference for other components that worked with UIImage (for example, UIImageView). I&#8217;d just like to notice that the way I found after a long search, gave pixelated image output when run on a retina display. This was fixed by replacing the function UIGraphicsBeginImageContext UIGraphicsBeginImageContextWithOptionsTotal. Finally we have the following code::<\/p>\n<pre lang=\"objc\">- (UIImage *) imageByName:(NSString *)name withColor:(UIColor *)color {\r\n    \/\/ load the image\r\n    UIImage *img =  = [UIImage imageWithContentsOfFile:name];\r\n\r\n    \/\/ begin a new image context, to draw our colored image onto\r\n\/\/    UIGraphicsBeginImageContext(img.size);\r\n    UIGraphicsBeginImageContextWithOptions(img.size, NO, [UIScreen mainScreen].scale);\r\n    \/\/ get a reference to that context we created\r\n    CGContextRef context = UIGraphicsGetCurrentContext();\r\n\r\n    \/\/ set the fill color\r\n    [color setFill];\r\n\r\n    \/\/ translate\/flip the graphics context (for transforming from CG* coords to UI* coords\r\n    CGContextTranslateCTM(context, 0, img.size.height);\r\n    CGContextScaleCTM(context, 1.0, -1.0);\r\n\r\n    CGContextSetBlendMode(context, kCGBlendModeMultiply);\r\n    CGRect rect = CGRectMake(0, 0, img.size.width, img.size.height);\r\n    CGContextDrawImage(context, rect, img.CGImage);\r\n\r\n    \/\/ set a mask that matches the shape of the image, then draw (color burn) a colored rectangle\r\n    CGContextClipToMask(context, rect, img.CGImage);\r\n    CGContextAddRect(context, rect);\r\n    CGContextDrawPath(context,kCGPathFill);\r\n\r\n    \/\/ generate a new UIImage from the graphics context we drew onto\r\n    UIImage *coloredImg = UIGraphicsGetImageFromCurrentImageContext();\r\n    UIGraphicsEndImageContext();\r\n\r\n    \/\/return the color-burned image\r\n    return coloredImg;\r\n}<\/pre>\n<p><!--:--><!--:ru--><\/p>\n<p>\u0423 \u043c\u0435\u043d\u044f \u0441\u0442\u043e\u044f\u043b\u0430 \u0437\u0430\u0434\u0430\u0447\u0430 \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u0442\u044c \u0432\u0441\u0435 \u044d\u0442\u043e \u0434\u0435\u043b\u043e \u043a \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0443 UIButton, \u043d\u043e, \u043a\u0430\u043a \u043d\u0435 \u0441\u043b\u043e\u0436\u043d\u043e \u0434\u043e\u0433\u0430\u0434\u0430\u0442\u044c\u0441\u044f, \u043f\u0440\u0438\u043d\u0446\u0438\u043f\u0438\u0430\u043b\u044c\u043d\u043e \u044d\u0442\u043e \u043d\u0438\u0447\u0435\u043c \u043d\u0435 \u043e\u0442\u043b\u0438\u0447\u0430\u0435\u0442\u0441\u044f \u043f\u043e \u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u044e \u043a \u0434\u0440\u0443\u0433\u0438\u043c \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430\u043c, \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0449\u0438\u043c \u0441 \u043e\u0431\u044a\u0435\u043a\u0442\u0430\u043c\u0438 UIImage (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, UIImageView).<br \/>\n\u0421\u0440\u0430\u0437\u0443 \u0445\u043e\u0447\u0443 \u0437\u0430\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e \u0441\u043f\u043e\u0441\u043e\u0431, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u044f \u043d\u0430\u0448\u0435\u043b \u043f\u043e\u0441\u043b\u0435 \u0434\u043e\u043b\u0433\u0438\u0445 \u043f\u043e\u0438\u0441\u043a\u043e\u0432, \u0432\u044b\u0434\u0430\u0432\u0430\u043b \u043f\u0438\u043a\u0441\u0438\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0435 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u043d\u0430 \u0432\u044b\u0445\u043e\u0434\u0435 \u043f\u0440\u0438 \u0437\u0430\u043f\u0443\u0441\u043a\u0435 \u043d\u0430 \u0440\u0435\u0442\u0438\u043d\u0430-\u0434\u0438\u0441\u043f\u043b\u0435\u0435. \u042d\u0442\u043e \u0438\u0441\u043f\u0440\u0430\u0432\u0438\u043b\u043e\u0441\u044c \u0437\u0430\u043c\u0435\u043d\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 UIGraphicsBeginImageContext \u043d\u0430 UIGraphicsBeginImageContextWithOptions<br \/>\n\u0418\u0442\u043e\u0433\u043e \u0438\u043c\u0435\u0435\u043c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u043a\u043e\u0434:<\/p>\n<pre lang=\"objc\">- (UIImage *) imageByName:(NSString *)name withColor:(UIColor *)color {\r\n    \/\/ load the image\r\n    UIImage *img =  = [UIImage imageWithContentsOfFile:name];\r\n\r\n    \/\/ begin a new image context, to draw our colored image onto\r\n\/\/    UIGraphicsBeginImageContext(img.size);\r\n    UIGraphicsBeginImageContextWithOptions(img.size, NO, [UIScreen mainScreen].scale);\r\n    \/\/ get a reference to that context we created\r\n    CGContextRef context = UIGraphicsGetCurrentContext();\r\n\r\n    \/\/ set the fill color\r\n    [color setFill];\r\n\r\n    \/\/ translate\/flip the graphics context (for transforming from CG* coords to UI* coords\r\n    CGContextTranslateCTM(context, 0, img.size.height);\r\n    CGContextScaleCTM(context, 1.0, -1.0);\r\n\r\n    CGContextSetBlendMode(context, kCGBlendModeMultiply);\r\n    CGRect rect = CGRectMake(0, 0, img.size.width, img.size.height);\r\n    CGContextDrawImage(context, rect, img.CGImage);\r\n\r\n    \/\/ set a mask that matches the shape of the image, then draw (color burn) a colored rectangle\r\n    CGContextClipToMask(context, rect, img.CGImage);\r\n    CGContextAddRect(context, rect);\r\n    CGContextDrawPath(context,kCGPathFill);\r\n\r\n    \/\/ generate a new UIImage from the graphics context we drew onto\r\n    UIImage *coloredImg = UIGraphicsGetImageFromCurrentImageContext();\r\n    UIGraphicsEndImageContext();\r\n\r\n    \/\/return the color-burned image\r\n    return coloredImg;\r\n}<\/pre>\n<p><!--:--><\/p>\n","protected":false},"excerpt":{"rendered":"<p>For a very long time I couldn&#8217;t solve one problem that appeared time by time. Until it happened again a few day ago. But this time I decided that to put up with this state of affairs is not more possible and once again began to research. And the problem is the following. Assume you [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[47,1],"tags":[128,61],"class_list":["post-1116","post","type-post","status-publish","format-standard","hentry","category-coding","category-news","tag-coregraphics","tag-ios"],"_links":{"self":[{"href":"https:\/\/hxml.me\/blog\/wp-json\/wp\/v2\/posts\/1116","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hxml.me\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hxml.me\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hxml.me\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/hxml.me\/blog\/wp-json\/wp\/v2\/comments?post=1116"}],"version-history":[{"count":10,"href":"https:\/\/hxml.me\/blog\/wp-json\/wp\/v2\/posts\/1116\/revisions"}],"predecessor-version":[{"id":1141,"href":"https:\/\/hxml.me\/blog\/wp-json\/wp\/v2\/posts\/1116\/revisions\/1141"}],"wp:attachment":[{"href":"https:\/\/hxml.me\/blog\/wp-json\/wp\/v2\/media?parent=1116"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hxml.me\/blog\/wp-json\/wp\/v2\/categories?post=1116"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hxml.me\/blog\/wp-json\/wp\/v2\/tags?post=1116"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}