App開發流程之圖像處理工具類

来源:http://www.cnblogs.com/ALongWay/archive/2016/09/13/5869561.html
-Advertisement-
Play Games

先羅列一下工具類中提供的方法: 說明: 1.獲取毛玻璃效果(高斯模糊)圖像 在iOS8以後,提供了類UIBlurEffect、UIVisualEffectView,可以方便的生成高斯模糊的視圖,然後只需要作為目標視圖的subview即可看到效果。在iOS7上需要自行實現,不過蘋果在WWDC 2013 ...


先羅列一下工具類中提供的方法:

/**
 *  根據原始view和毛玻璃樣式,獲取模糊視圖,並自動作為原view的subview(如果不需要作為子視圖,自行調用removeFromSuperview)
 */
+ (UIView *)getBlurEffectViewWithOriginalView:(UIView *)originalView style:(ImageHelperBlurEffectStyle)style;

/**
 *  根據原始圖像和毛玻璃樣式,獲取新圖像
 */
+ (UIImage *)getBlurEffectImageWithOriginalImage:(UIImage *)originalImage style:(ImageHelperBlurEffectStyle)style;

/**
 *  根據原始圖像,等比縮放繫數,得到新圖像
 */
+ (UIImage *)getImageWithOriginalImage:(UIImage *)originalImage scale:(CGFloat)scale;

/**
 *  根據原始圖像,等比縮放最大尺寸,得到新圖像
 */
+ (UIImage *)getImageWithOriginalImage:(UIImage *)originalImage scaleMaxSize:(CGSize)scaleMaxSize;

/**
 *  根據原始圖像,等比縮放最大尺寸,得到新尺寸
 */
+ (CGSize)getImageSizeWithOriginalImage:(UIImage *)originalImage scaleMaxSize:(CGSize)scaleMaxSize;

/**
 *  根據原始圖像,完全填充尺寸,得到新圖像
 */
+ (UIImage *)getImageWithOriginalImage:(UIImage *)originalImage fillSize:(CGSize)fillSize;

/**
 *  根據原始圖像,裁剪區域,得到新圖像
 */
+ (UIImage *)getImageWithOriginalImage:(UIImage *)originalImage cutFrame:(CGRect)cutFrame;

/**
 *  根據顏色,得到單位尺寸的純色新圖像
 */
+ (UIImage *)getImageWithColor:(UIColor *)color;

/**
 *  根據view,得到快照
 */
+ (UIImage *)getSnapshotWithView:(UIView *)view;

/**
 *  全屏截圖,但不包括狀態欄
 */
+ (UIImage *)getFullScreenSnapshot;

說明:

1.獲取毛玻璃效果(高斯模糊)圖像

在iOS8以後,提供了類UIBlurEffect、UIVisualEffectView,可以方便的生成高斯模糊的視圖,然後只需要作為目標視圖的subview即可看到效果。在iOS7上需要自行實現,不過蘋果在WWDC 2013上提供了一個UIImage+ImageEffects的分類,可以生成高斯模糊的圖像。將分類加入項目Categories目錄後,在ImageHelper中引用。因為UIBlurEffectStyle是在iOS8以後出現的,所以自定義了一個與其對應的枚舉類型ImageHelperBlurEffectStyle,以便在iOS7中也可以正常使用。實現代碼如下:

+ (UIView *)getBlurEffectViewWithOriginalView:(UIView *)originalView style:(ImageHelperBlurEffectStyle)style
{
    if (DeviceIOSVersionAbove(8)) {
        UIBlurEffectStyle blurStyle;
        
        switch (style) {
            case ImageHelperBlurEffectStyleExtraLight: {
                blurStyle = UIBlurEffectStyleExtraLight;
                break;
            }
            case ImageHelperBlurEffectStyleLight: {
                blurStyle = UIBlurEffectStyleLight;
                break;
            }
            case ImageHelperBlurEffectStyleDark: {
                blurStyle = UIBlurEffectStyleDark;
                break;
            }
        }
        
        UIBlurEffect *effect = [UIBlurEffect effectWithStyle:blurStyle];
        UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:effect];
        effectView.frame = originalView.bounds;
        [originalView addSubview:effectView];

        return effectView;
    } else {
        UIImage *originalImage = [self getSnapshotWithView:originalView];
        UIImage *blurImage = [self getBlurEffectImageWithOriginalImage:originalImage style:style];
        
        UIImageView *effectView = [[UIImageView alloc] initWithFrame:originalView.bounds];
        [effectView setImage:blurImage];
        
        [originalView addSubview:effectView];
        
        return effectView;
    }
}

+ (UIImage *)getBlurEffectImageWithOriginalImage:(UIImage *)originalImage style:(ImageHelperBlurEffectStyle)style
{
    UIImage *newImage;
    
    switch (style) {
        case ImageHelperBlurEffectStyleExtraLight: {
            newImage = [originalImage applyExtraLightEffect];
            break;
        }
        case ImageHelperBlurEffectStyleLight: {
            newImage = [originalImage applyLightEffect];
            break;
        }
        case ImageHelperBlurEffectStyleDark: {
            newImage = [originalImage applyDarkEffect];
            break;
        }
    }
    
    return newImage;
}

 

2.提供了將圖像等比例縮放的系列方法,以及裁剪的方法。基本思路就是在當前圖像上下文中,指定繪製尺寸(即點陣圖的尺寸),然後將相應圖像繪製到指定位置,然後生成最終圖像。例如裁剪圖像示例代碼:

+ (UIImage *)getImageWithOriginalImage:(UIImage *)originalImage cutFrame:(CGRect)cutFrame
{
    CGSize newSize = cutFrame.size;
    
    UIGraphicsBeginImageContext(newSize);
    [originalImage drawInRect:CGRectMake(-cutFrame.origin.x, -cutFrame.origin.y, cutFrame.size.width, cutFrame.size.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return newImage;
}

 

3.截屏方法

需要用到方法:

UIGraphicsBeginImageContextWithOptions(CGSize size, BOOL opaque, CGFloat scale)。第一個參數仍然是指定圖像上下文繪製尺寸;第二個參數指定是否不透明;第三個為等比例縮放繫數,如果為0.0,表示與設備主屏幕的繫數一致。

CALayer的renderInContext:(CGContextRef)ctx方法,將圖層全部渲染到某個上下文中,建議為當前圖像上下文。最後得到圖像。

+ (UIImage *)getSnapshotWithView:(UIView *)view
{
    UIGraphicsBeginImageContextWithOptions(view.bounds.size, YES, 0.0);
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return newImage;
}

+ (UIImage *)getFullScreenSnapshot
{
    return [self getSnapshotWithView:[UIApplication sharedApplication].keyWindow];
}

全屏截圖時候,不包括狀態欄,因為狀態欄不在應用的window上,無法直接獲取。但是可以通過私有Api拿到系統截屏圖像,在此不展開探究。

 

測試內容

    UIImage *icon = LOADIMAGE(AppIcon);
    
    UIImage *testImg;
    testImg = [ImageHelper getImageWithOriginalImage:icon scale:2];
    LOG(@"%@", testImg);
    testImg = [ImageHelper getImageWithOriginalImage:icon scaleMaxSize:CGSizeMake(100, 90)];
    LOG(@"%@", testImg);
    testImg = [ImageHelper getImageWithOriginalImage:icon fillSize:CGSizeMake(100, 90)];
    LOG(@"%@", testImg);
    testImg = [ImageHelper getImageWithOriginalImage:icon cutFrame:CGRectMake(10, 10, 50, 50)];
    LOG(@"%@", testImg);
    testImg = [ImageHelper getImageWithColor:COLOR(255, 120, 100)];
    LOG(@"%@", testImg);
    testImg = [ImageHelper getSnapshotWithView:self.view];
    LOG(@"%@", testImg);
    testImg = [ImageHelper getFullScreenSnapShot];
    LOG(@"%@", testImg);
    testImg = [ImageHelper getBlurEffectImageWithOriginalImage:testImg style:ImageHelperBlurEffectStyleDark];
    LOG(@"%@", testImg);
    
    UIView *coverView = [ImageHelper getBlurEffectViewWithOriginalView:[UIApplication sharedApplication].keyWindow style:ImageHelperBlurEffectStyleDark];
2016-09-13 19:05:11.995 base[33087:2301853] <UIImage: 0x7ffaf97e8d00>, {120, 120}
2016-09-13 19:05:11.997 base[33087:2301853] <UIImage: 0x7ffaf97e9610>, {90, 90}
2016-09-13 19:05:11.999 base[33087:2301853] <UIImage: 0x7ffaf950a330>, {100, 90}
2016-09-13 19:05:12.001 base[33087:2301853] <UIImage: 0x7ffaf9463630>, {50, 50}
2016-09-13 19:05:12.002 base[33087:2301853] <UIImage: 0x7ffaf950a330>, {1, 1}
2016-09-13 19:05:12.007 base[33087:2301853] <UIImage: 0x7ffaf96004b0>, {375, 667}
2016-09-13 19:05:12.013 base[33087:2301853] <UIImage: 0x7ffaf950a330>, {375, 667}
2016-09-13 19:05:12.040 base[33087:2301853] <UIImage: 0x7ffaf9506f30>, {375, 667}

 

1.可以單步調試代碼,查看testImg圖像內容:

 

2.[ImageHelper getSnapshotWithView:self.view];這行代碼中的self.view生成快照後,尺寸為{375, 667},但如果在viewDidLoad方法中加入代碼:

self.edgesForExtendedLayout = UIRectEdgeNone;

重新輸出的尺寸為{375, 603},因為edgesForExtendedLayout屬性預設為UIRectEdgeAll,這在處理UI佈局時候需要註意。後續記錄的UIViewContoller父類也會提到。

 

base項目已更新:[email protected]:ALongWay/base.git


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 在資料庫獲取一些新聞時,有時文字過多,為了不破壞佈局,常常需要只顯示一部分,這是需要用到文字截斷的功能。雖然css也可以實現,但是限制太多 css實現需要text-overflow:ellipsis;overflow:hidden;white-space:nowrap;三個屬性,另外還需要文本標簽寬 ...
  • CSS3實現輪播圖主要是由css:background-position和css3:animation實現。且實現此輪播需要一張四個圖橫著相連的圖片。 註(Internet Explorer 10、Firefox 以及 Opera 支持 animation 屬性。Safari 和 Chrome 支持 ...
  • 給自己定一個小目標,一個禮拜完成100個inkscape案例。 今天完成picture on the wall ...
  • 前言: 最近一個多月在認真的學習Android和做項目,文章內容表達的不好或者理解錯了,希望大家評論指出。 :-) 本文是總結幾個比較常用且使用的技巧,和一個大家都會遇到的問題。 文章中大部分語句摘抄自一下兩篇大神寫的文章:(如果對ExpandableListView一無所知,建議按照順序去閱讀,遇 ...
  • 為什麼要用Kotlin,和Java完全相容,相互之間引用完全不是問題,所有沒有什麼負擔。 使用Kotlin已經近一個月,基本上看完了語法就上正式項目了,期間幾次準備放棄,最終堅持下來了。關於Kotlin的優點,不必多說了,基於這一個月的經驗,來說說Kotlin的入門和坑。 配置工程 添加依賴 作為一 ...
  • API 23之前的版本都是自動獲取許可權,而從 Android 6.0 開始添加了許可權申請的需求,更加安全。 這裡以單個存儲許可權為例: · 在 Manifest 中添加訪問許可權:(只需設置可寫,因為可寫必定可讀) · 動態申請許可權的過程: ...
  • SQLite SQLite是一種超輕量級的嵌入式資料庫,大小隻有幾百KB,但是其語法支持標準SQL語法,同時還遵循了資料庫的ACID事務,所以學過其他資料庫的開發人員都很容易掌握其使用。 sql語法就不介紹了,直接看在android中的使用 SQLiteOpenHelper——封裝好的資料庫操作輔助 ...
  • iOS Swift 註釋與分號 註釋 註釋是每門語言都存在的一種解釋方式,Swift的註釋與C語言的註釋非常相似,單行註釋採用//. 但是,與C語言多行註釋不同,Swift的多行註釋可以嵌套在其它的多行註釋之中. 運用嵌套多行註釋可以註釋一大段代碼,並且包括相應的註釋. 分號 與其他大部分編程語言不 ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...