2015-02-10 64 views
24

我從服務器接收圖像,然後基於用戶選擇的顏色,圖像顏色將發生變化。如何更改圖像tintColor

我試過如下:

_sketchImageView.image = [_sketchImageView.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]; 
[_sketchImageView setTintColor:color]; 

我得到了我的目標相反的(白色的UIImage外面是彩色的與所選擇的顏色)。

出了什麼問題?

我需要在這個問題上做同樣的事情,所提供的解決方案並不能解決我的問題。 How can I change image tintColor in iOS and WatchKit

+0

如果你想填補圖像與一些顏色,你需要更先進的解決方案。像這樣:https://github.com/mxcl/UIImageWithColor – orkenstein 2015-02-10 09:19:56

+0

我認爲你需要背景顏色的效果,試試'[_sketchImageView setBackgroundColor:color]' – Saif 2015-02-10 09:22:20

+0

我以前試過這個解決方案,它給出了相同的結果。 // UIImage * img = [UIImage resizableImageWithColor:color cornerRadius:10]; // UIImage * img = [UIImage imageWithColor:color size:CGSizeMake(20,20)]; UIImage * img = [UIImage imageWithColor:color]; _sketchImageView.image = img; – user2168496 2015-02-10 09:36:34

回答

45

嘗試生成新的形象爲自己

UIImage *newImage = [_sketchImageView.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]; 
UIGraphicsBeginImageContextWithOptions(image.size, NO, newImage.scale); 
[yourTintColor set]; 
[newImage drawInRect:CGRectMake(0, 0, image.size.width, newImage.size.height)]; 
newImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

_sketchImageView.image = newImage; 

並使用它。

好運

======= ======= UPDATE

該解決方案將只有所有像素的圖像的變色。

實施例:我們有一個書圖像:http://pngimg.com/upload/book_PNG2113.png

enter image description here

並運行上面的代碼後(EXP:TintColor是紅色)。我們有:

enter image description here

SO:你的形象是如何取決於你如何把它設計

+0

thanx很多,可以說我有一本書的圖像,這個解決方案給圖書的邊界以外的圖像着色..我想的是相反的,即書本身是彩色的。 任何想法爲什麼會發生這種情況? – user2168496 2015-02-11 08:08:07

+0

我認爲你需要重新設計你的書籍圖像。我剛剛編輯了我的帖子。看到它 – VietHung 2015-02-11 08:34:46

+0

Yeah.yessss ..我試圖你的形象,它工作正常:)我真的很感謝 – user2168496 2015-02-11 08:48:50

2

你可以試試:

_sketchImageView.image = [self imageNamed:@"imageName" withColor:[UIColor blackColor]]; 

- (UIImage *)imageNamed:(NSString *)name withColor:(UIColor *)color 
{ 
    // load the image 
    //NSString *name = @"badge.png"; 
    UIImage *img = [UIImage imageNamed:name]; 

    // begin a new image context, to draw our colored image onto 
    UIGraphicsBeginImageContext(img.size); 

    // get a reference to that context we created 
    CGContextRef context = UIGraphicsGetCurrentContext(); 

    // set the fill color 
    [color setFill]; 

    // translate/flip the graphics context (for transforming from CG* coords to UI* coords 
    CGContextTranslateCTM(context, 0, img.size.height); 
    CGContextScaleCTM(context, 1.0, -1.0); 

    // set the blend mode to color burn, and the original image 
    CGContextSetBlendMode(context, kCGBlendModeColorBurn); 
    CGRect rect = CGRectMake(0, 0, img.size.width, img.size.height); 
    CGContextDrawImage(context, rect, img.CGImage); 

    // set a mask that matches the shape of the image, then draw (color burn) a colored rectangle 
    CGContextClipToMask(context, rect, img.CGImage); 
    CGContextAddRect(context, rect); 
    CGContextDrawPath(context,kCGPathFill); 

    // generate a new UIImage from the graphics context we drew onto 
    UIImage *coloredImg = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    //return the color-burned image 
    return coloredImg; 
} 
+0

此代碼給CGContextDrawImage:無效的上下文0x0。這是一個嚴重的錯誤。此應用程序或其使用的庫正在使用無效的上下文,從而導致系統穩定性和可靠性的整體降級。這個通知是禮貌的:請解決這個問題。這將成爲即將到來的更新中的致命錯誤。 任何想法plz? – user2168496 2015-02-10 12:09:46

+0

檢查您的UIImageView實例是否不爲空。如果該方法未針對空的UIIMageView調用。 – gabriel 2015-02-10 12:15:33

+0

是有圖像,圖像顯示在圖像視圖中,然後在用戶選擇顏色後,我想要更改它的顏色 – user2168496 2015-02-10 12:47:18

4

嘗試是這樣的

UIImage *originalImage = _sketchImageView.image 
UIImage *newImage = [originalImage imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]; 
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,50,50)]; // your image size 
imageView.tintColor = [UIColor redColor]; // or whatever color that has been selected 
imageView.image = newImage; 
_sketchImageView.image = imageView.image; 

希望這有助於。

+0

thanx的幫助,但這將圖像更改爲白色圖像(因爲它是一個空白的) – user2168496 2015-02-10 12:45:10

+0

所以你說你的_sketchImageView.image是空白的? – Gismay 2015-02-10 13:22:30

+0

不,這不是..我的意思是在應用你的代碼後,我得到一個白色的圖像 – user2168496 2015-02-10 13:39:50

1

嘗試在圖像視圖的超視圖上設置色調顏色。例如。 [self.view setTintColor:color];

16

在斯威夫特你可以使用這個擴展:基於@ VietHung的Objective-C的解決方案]


extension UIImage { 
    func imageWithColor(color: UIColor) -> UIImage? { 
     var image = imageWithRenderingMode(.AlwaysTemplate) 
     UIGraphicsBeginImageContextWithOptions(size, false, scale) 
     color.set() 
     image.drawInRect(CGRect(x: 0, y: 0, width: size.width, height: size.height)) 
     image = UIGraphicsGetImageFromCurrentImageContext() 
     UIGraphicsEndImageContext() 
     return image 
    } 
} 
0

下面是我如何在Swift中應用和使用IOS 9中的色彩。

//apply a color to an image 
//ref - http://stackoverflow.com/questions/28427935/how-can-i-change-image-tintcolor 
//ref - https://www.captechconsulting.com/blogs/ios-7-tutorial-series-tint-color-and-easy-app-theming 
func getTintedImage() -> UIImageView { 

    var image  : UIImage; 
    var imageView : UIImageView; 

    image = UIImage(named: "someAsset")!; 
    let size : CGSize = image.size; 
    let frame : CGRect = CGRectMake((UIScreen.mainScreen().bounds.width-86)/2, 600, size.width, size.height); 

    let redCover : UIView = UIView(frame: frame); 

    redCover.backgroundColor = UIColor.redColor(); 
    redCover.layer.opacity = 0.75; 

    imageView  = UIImageView(); 
    imageView.image = image.imageWithRenderingMode(UIImageRenderingMode.Automatic); 

    imageView.addSubview(redCover); 

    return imageView; 
} 
3

在斯威夫特3.0你可以使用這個擴展:

extension UIImage { 
    func imageWithColor(_ color: UIColor) -> UIImage? { 
     var image = imageWithRenderingMode(.alwaysTemplate) 
     UIGraphicsBeginImageContextWithOptions(size, false, scale) 
     color.set() 
     image.draw(in: CGRect(x: 0, y: 0, width: size.width, height: size.height)) 
     image = UIGraphicsGetImageFromCurrentImageContext() 
     UIGraphicsEndImageContext() 
     return image 
    } 
} 
12

在SWIFT 2上@ VietHung的Objective-C的解決方案基於]。0您可以使用此

let image = UIImage(named:"your image name")?.imageWithRenderingMode(.AlwaysTemplate) 
let yourimageView.tintColor = UIColor.redColor() 
yourimageView.image = image 

在SWIFT 3.0,你可以使用這個

let image = UIImage(named:"your image name")?.withRenderingMode(.alwaysTemplate) 
let yourimageView.tintColor = UIColor.red 
yourimageView.image = image 
+2

對於Swift 3。0,你會想使用withRenderingMode,而不是imageWithRenderingMode。 – Joe 2017-03-22 03:54:49

1

對於雨燕3.0,我做的UIImageView的自定義子類,稱爲TintedUIImageView。現在,像使用任何色調的顏色在界面生成器或代碼

class TintedUIImageView: UIImageView { 

    override func awakeFromNib() { 
     if let image = self.image { 
      self.image = image.withRenderingMode(.alwaysTemplate) 
     } 
    } 
} 
+0

歡迎來到SO!您的回覆看起來像是評論而不是答案。一旦你有足夠的[聲譽](http://stackoverflow.com/help/whats-reputation),你將可以在任何帖子上[評論](http://stackoverflow.com/help/privileges/comment)。另外檢查這[我可以做什麼,而不是](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-c​​an-i-do-instead )。如果您打算回答,請閱讀[如何回答](http://stackoverflow.com/help/how-to-answer)以遵循SO指導原則。 – thewaywewere 2017-05-05 19:55:21

0

一兩件事你可以做的,只是你的圖像添加到Assets文件夾中的XCode,然後更改渲染模式,模板圖像設定,所以每當你改變UIImageView的色彩顏色,它會自動改變圖像。

檢查此鏈接了 - >https://www.google.co.in/url?sa=i&rct=j&q=&esrc=s&source=images&cd=&cad=rja&uact=8&ved=0ahUKEwiM0YXO0ejTAhUIQ48KHfGpBpgQjRwIBw&url=https%3A%2F%2Fkrakendev.io%2Fblog%2F4-xcode-asset-catalog-secrets-you-need-to-know&psig=AFQjCNGnAzVn92pCqM8612o1R0J9q1y7cw&ust=1494619445516498