Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Saturday, October 29, 2016

How can I change image tintColor in iOS and WatchKit

How can I change image tintColor in iOS and WatchKit


I have an UIImageView called "theImageView", with UIImage in a single color (transparent background) just like the left black heart below. How can I change the tint color of this image programmatically in iOS 7 or above, as per the tint method used in the iOS 7+ Navigation Bar icons?

Can this method also work in WatchKit for an Apple Watch app?

enter image description here

Answer by iamamused for How can I change image tintColor in iOS and WatchKit


Here's a category that should do the trick

@interface UIImage(Overlay)  @end    @implementation UIImage(Overlay)    - (UIImage *)imageWithColor:(UIColor *)color1  {          UIGraphicsBeginImageContextWithOptions(self.size, NO, self.scale);          CGContextRef context = UIGraphicsGetCurrentContext();          CGContextTranslateCTM(context, 0, self.size.height);          CGContextScaleCTM(context, 1.0, -1.0);          CGContextSetBlendMode(context, kCGBlendModeNormal);          CGRect rect = CGRectMake(0, 0, self.size.width, self.size.height);          CGContextClipToMask(context, rect, self.CGImage);          [color1 setFill];          CGContextFillRect(context, rect);          UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();          UIGraphicsEndImageContext();          return newImage;  }  @end  

so you would do:

theImageView.image = [theImageView.image imageWithColor:[UIColor redColor]];  

Answer by fuzz for How can I change image tintColor in iOS and WatchKit


I had to do this in Swift using an extension.

I thought I'd share how I did it:

extension UIImage {      func imageWithColor(color1: UIColor) -> UIImage {          UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale)          color1.setFill()            let context = UIGraphicsGetCurrentContext() as CGContextRef          CGContextTranslateCTM(context, 0, self.size.height)          CGContextScaleCTM(context, 1.0, -1.0);          CGContextSetBlendMode(context, CGBlendMode.Normal)            let rect = CGRectMake(0, 0, self.size.width, self.size.height) as CGRect          CGContextClipToMask(context, rect, self.CGImage)          CGContextFillRect(context, rect)            let newImage = UIGraphicsGetImageFromCurrentImageContext() as UIImage          UIGraphicsEndImageContext()            return newImage      }  }  

Usage:

theImageView.image = theImageView.image.imageWithColor(UIColor.redColor())

Answer by yazh for How can I change image tintColor in iOS and WatchKit


Try this

http://robots.thoughtbot.com/designing-for-ios-blending-modes

or

- (void)viewDidLoad  {  [super viewDidLoad];    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 30, 300, 50)];  label.numberOfLines = 0;  label.font = [UIFont systemFontOfSize:13];  label.text = @"These checkmarks use the same gray checkmark image with a tintColor applied to the image view";  [self.view addSubview:label];    [self _createImageViewAtY:100 color:[UIColor purpleColor]];  }    - (void)_createImageViewAtY:(int)y color:(UIColor *)color {  UIImage *image = [[UIImage imageNamed:@"gray checkmark.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];  UIImageView *imageView = [[UIImageView alloc] initWithImage:image];  CGRect frame = imageView.frame;  frame.origin.x = 100;  frame.origin.y = y;  imageView.frame = frame;    if (color)      imageView.tintColor = color;    [self.view addSubview:imageView];  }  

Answer by Duncan Babbage for How can I change image tintColor in iOS and WatchKit


iOS
For an iOS app, in Swift 3:

theImageView.image = theImageView.image!.withRenderingMode(.alwaysTemplate)  theImageView.tintColor = UIColor.red  

Swift 2:

theImageView.image = theImageView.image!.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate)  theImageView.tintColor = UIColor.redColor()  

Meanwhile, the modern Objective-C solution is:

theImageView.image = [theImageView.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];  [theImageView setTintColor:[UIColor redColor]];  

Watchkit
In WatchKit for Apple Watch apps, you can set the tint color for a template image.

  1. You must add your image to an Asset Catalog in your WatchKit App, and set the image set to be rendered as a Template Image in the Attributes Inspector. Unlike for an iPhone app, you cannot set the template rendering in code in the WatchKit Extension at present.
  2. Set that image to be used in your WKInterfaceImage in interface builder for your app
  3. Create an IBOutlet in your WKInterfaceController for the WKInterfaceImage called 'theImage'...

To then set the tint color in Swift 3:

theImage.setTintColor(UIColor.red)  

Swift 2:

theImage.setTintColor(UIColor.redColor())  

To then set the tint color in Objective-C:

[self.theImage setTintColor:[UIColor redColor]];  

If you use a template image and do not apply a tint colour, the Global Tint for your WatchKit app will be applied. If you have not set a Global Tint, theImage will be tinted light blue by default when used as a template image.

Answer by Esqarrouth for How can I change image tintColor in iOS and WatchKit


With Swift

let commentImageView = UIImageView(frame: CGRectMake(100, 100, 100, 100))  commentImageView.image = UIImage(named: "myimage.png")!.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate)  commentImageView.tintColor = UIColor.blackColor()  addSubview(commentImageView)  

Answer by JerryZhou for How can I change image tintColor in iOS and WatchKit


In storyboard and image Assets. you can change this two also:

Update the Render Mode to Template Image

Update the Render Mode to Template Image in Image Assets

Update the tint Color in Views.

Update the tint Color in Views in Views

Answer by YannickSteph for

How can I change image tintColor in iOS and WatchKit

For change tint of image (pick, classical image, photo) use that :

Example image : enter image description here enter image description here

Swift 2

public extension UIImage {      /**       Tint, Colorize image with given tint color

This is similar to Photoshop's "Color" layer blend mode

This is perfect for non-greyscale source images, and images that have both highlights and shadows that should be preserved

white will stay white and black will stay black as the lightness of the image is preserved

**To** - parameter tintColor: UIColor - returns: UIImage */ public func tintPhoto(tintColor: UIColor) -> UIImage { return modifiedImage { context, rect in // draw black background - workaround to preserve color of partially transparent pixels CGContextSetBlendMode(context, .Normal) UIColor.blackColor().setFill() CGContextFillRect(context, rect) // draw original image CGContextSetBlendMode(context, .Normal) CGContextDrawImage(context, rect, self.CGImage) // tint image (loosing alpha) - the luminosity of the original image is preserved CGContextSetBlendMode(context, .Color) tintColor.setFill() CGContextFillRect(context, rect) // mask by alpha values of original image CGContextSetBlendMode(context, .DestinationIn) CGContextDrawImage(context, rect, self.CGImage) } } /** Tint Picto to color - parameter fillColor: UIColor - returns: UIImage */ public func tintPicto(fillColor: UIColor) -> UIImage { return modifiedImage { context, rect in // draw tint color CGContextSetBlendMode(context, .Normal) fillColor.setFill() CGContextFillRect(context, rect) // mask by alpha values of original image CGContextSetBlendMode(context, .DestinationIn) CGContextDrawImage(context, rect, self.CGImage) } } /** Modified Image Context, apply modification on image - parameter draw: (CGContext, CGRect) -> ()) - returns: UIImage */ private func modifiedImage(@noescape draw: (CGContext, CGRect) -> ()) -> UIImage { // using scale correctly preserves retina images UIGraphicsBeginImageContextWithOptions(size, false, scale) let context: CGContext! = UIGraphicsGetCurrentContext() assert(context != nil) // correctly rotate image CGContextTranslateCTM(context, 0, size.height); CGContextScaleCTM(context, 1.0, -1.0); let rect = CGRectMake(0.0, 0.0, size.width, size.height) draw(context, rect) let image = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return image } }

UPD

Swift 3

extension UIImage {        /**       Tint, Colorize image with given tint color

This is similar to Photoshop's "Color" layer blend mode

This is perfect for non-greyscale source images, and images that have both highlights and shadows that should be preserved

white will stay white and black will stay black as the lightness of the image is preserved

**To** - parameter tintColor: UIColor - returns: UIImage */ func tintPhoto(_ tintColor: UIColor) -> UIImage { return modifiedImage { context, rect in // draw black background - workaround to preserve color of partially transparent pixels context.setBlendMode(.normal) UIColor.black.setFill() context.fill(rect) // draw original image context.setBlendMode(.normal) context.draw(cgImage!, in: rect) // tint image (loosing alpha) - the luminosity of the original image is preserved context.setBlendMode(.color) tintColor.setFill() context.fill(rect) // mask by alpha values of original image context.setBlendMode(.destinationIn) context.draw(context.makeImage()!, in: rect) } } /** Tint Picto to color - parameter fillColor: UIColor - returns: UIImage */ func tintPicto(_ fillColor: UIColor) -> UIImage { return modifiedImage { context, rect in // draw tint color context.setBlendMode(.normal) fillColor.setFill() context.fill(rect) // mask by alpha values of original image context.setBlendMode(.destinationIn) context.draw(cgImage!, in: rect) } } /** Modified Image Context, apply modification on image - parameter draw: (CGContext, CGRect) -> ()) - returns: UIImage */ fileprivate func modifiedImage(_ draw: (CGContext, CGRect) -> ()) -> UIImage { // using scale correctly preserves retina images UIGraphicsBeginImageContextWithOptions(size, false, scale) let context: CGContext! = UIGraphicsGetCurrentContext() assert(context != nil) // correctly rotate image context.translateBy(x: 0, y: size.height) context.scaleBy(x: 1.0, y: -1.0) let rect = CGRect(x: 0.0, y: 0.0, width: size.width, height: size.height) draw(context, rect) let image = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return image! } }

Answer by Josep Escobar for How can I change image tintColor in iOS and WatchKit


Now i use this method based in Duncan Babbage response:

+ (UIImageView *) tintImageView: (UIImageView *)imageView withColor: (UIColor*) color{      imageView.image = [imageView.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];      [imageView setTintColor:color];      return imageView;  }  

Answer by DairySeeker for How can I change image tintColor in iOS and WatchKit


For swift 3 purposes

theImageView.image = theImageView.image!.withRenderingMode(.alwaysTemplate) theImageView.tintColor = UIColor.red


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

Related Posts:

0 comments:

Post a Comment

Popular Posts

Fun Page

Powered by Blogger.