貓貓學iOS 之微博項目實戰(3)微博主架構-UIImage防止iOS7之後自動渲染_定義分類,-uiimageios7

來源:互聯網
上載者:User

貓貓學iOS 之微博項目實戰(3)微博主架構-UIImage防止iOS7之後自動渲染_定義分類,-uiimageios7

貓貓分享,必須精品

原創文章,歡迎轉載。轉載請註明:翟乃玉的部落格
地址:http://blog.csdn.net/u013357243

一:效果對比

當我們設定tabBarController的tabBarItem.image的時候,預設情況下會出現圖片變成藍色的效果,這是因為ios7之後會對圖片自動渲染成藍色
代碼

 UIViewController *home = [[UIViewController alloc]init];     //設定標題    home.tabBarItem.title = @"首頁";    //設定未選中時候的圖片    home.tabBarItem.image = [UIImage imageNamed:@"tabbar_home"];     //設定選中時候的圖片    home.tabBarItem.selectedImage = [UIImage imageNamed:@"tabbar_home_selected"];    home.view.backgroundColor = [UIColor blueColor];

效果:

我們需要改成這樣的效果

二:解決方案

解決方案有兩個,一種是用xcode內建的功能設定,另一種是用純程式碼的方式來解決——之前貓貓見過自訂的tabBar。。。弱爆了(有增加了一點牛zhuang掰bi手段 )

第一種,代碼方式解決

代碼:

    //首頁    UIViewController *home = [[UIViewController alloc]init];    home.tabBarItem.title = @"首頁";    home.tabBarItem.image = [UIImage imageNamed:@"tabbar_home"];    UIImage *selImage = [UIImage imageNamed:@"tabbar_home_selected"];    //設定渲染模式為原始    home.tabBarItem.selectedImage = [selImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];    home.view.backgroundColor = [UIColor blueColor];

注意上下對比下原來代碼,

第二種方法

在圖片中找到圖片 第三個 –》 render as(渲染器) –》 original image(原始的圖片)

選中第二個 original image 就ok了 不過這方法要挨著設定

三:建立UIImage的分類

直接建立分類 一句代碼搞定

然後寫代碼了就是

UIImage+image.h中的代碼
////  UIImage+image.h//  貓貓微博////  Created by apple on 15-7-22.//  Copyright (c) 2015年 znycat. All rights reserved.//#import <UIKit/UIKit.h>@interface UIImage (image)/** *  載入最原始的圖片,沒有渲染過 * *  @param imageName 圖片名稱 * *  @return UIImage對象 */+(instancetype)imageWithOriginalName:(NSString *)imageName;@end
UIImage+image.m中的代碼
////  UIImage+image.m//  貓貓微博////  Created by apple on 15-7-22.//  Copyright (c) 2015年 znycat. All rights reserved.//#import "UIImage+image.h"@implementation UIImage (image)+(instancetype)imageWithOriginalName:(NSString *)imageName{    //ios7之後會自動渲染,不讓渲染有兩種方法    //1:在圖片中找到圖片 第三個 --》 render as(渲染器) --》 original image(原始的圖片)    //2:代碼設定    UIImage *image = [UIImage imageNamed:imageName];     //設定渲染模式為原始    return [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];}@end
四:調用

這樣調用的時候直接一句代碼就可以完成了

首先要在標頭檔匯入

#import "UIImage+image.h"

然後調用就這樣掉

    home.tabBarItem.selectedImage = [UIImage imageWithOriginalName:@"tabbar_home_selected"];

偶了

ps:注意代碼抽取,更好用,簡單說,兩句代碼能用一句寫,那就抽出來,以前貓貓就是覺得一兩句抽不抽的無所謂,複製下就行了,前段時間幫忙做項目吃了大虧 哭一個先,%>_<%

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.