亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在iOS和WatchKit中更改圖像tintColor

如何在iOS和WatchKit中更改圖像tintColor

收到一只叮咚 2019-09-18 13:29:02
我有一個名為“theImageView”的UIImageView,UIImage為單色(透明背景),就像下面的左黑心一樣。如何根據iOS 7+導航欄圖標中使用的色調方法在iOS 7或更高版本中以編程方式更改此圖像的色調?這種方法也適用于Apple Watch應用程序的WatchKit嗎?
查看完整描述

3 回答

?
動漫人物

TA貢獻1815條經驗 獲得超10個贊

這是一個應該做的技巧


@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

所以你會這樣做:


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


查看完整回答
反對 回復 2019-09-18
?
蠱毒傳說

TA貢獻1895條經驗 獲得超3個贊

我必須在Swift中使用extension。


我以為我會分享我是如何做到的:


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

    }

}

用法:


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


斯威夫特4


extension UIImage {

    func imageWithColor(color1: UIColor) -> UIImage {

        UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale)

        color1.setFill()


        let context = UIGraphicsGetCurrentContext()

        context?.translateBy(x: 0, y: self.size.height)

        context?.scaleBy(x: 1.0, y: -1.0)

        context?.setBlendMode(CGBlendMode.normal)


        let rect = CGRect(origin: .zero, size: CGSize(width: self.size.width, height: self.size.height))

        context?.clip(to: rect, mask: self.cgImage!)

        context?.fill(rect)


        let newImage = UIGraphicsGetImageFromCurrentImageContext()

        UIGraphicsEndImageContext()


        return newImage!

    }

}

用法:


theImageView.image = theImageView.image?.imageWithColor(color1: UIColor.red)


查看完整回答
反對 回復 2019-09-18
  • 3 回答
  • 0 關注
  • 685 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號