UIImage的各種處理(旋轉(zhuǎn),壓縮,寫入,讀去,計算大小)等和方法

UIImage的各種處理(旋轉(zhuǎn),壓縮,寫入,讀去,計算大小)等和方法的區(qū)別的介紹



第一:UIImage的詳細使用。

//

//? Image_Do_ViewController.m

//? UIImage_處理

//

//? Created by周雙建on 16/4/12.

//? Copyright ? 2016年周雙建. All rights reserved.

//

#import"Image_Do_ViewController.h"

#define APP_Width ? ? [UIScreen mainScreen].bounds.size.width

#define APP_Height? ? [UIScreen mainScreen].bounds.size.height

@interfaceImage_Do_ViewController()

@property(nonatomic,strong)UIImageView* IM_View;

@end

@implementationImage_Do_ViewController

- (void)viewDidLoad {

[superviewDidLoad];

self.navigationController.navigationBarHidden=NO;

self.navigationController.navigationBar.translucent=NO;

self.view.backgroundColor= [[UIColorwhiteColor]colorWithAlphaComponent:1.0f];

self.title=@"Succes QQ Bar-Provide";

[self.navigationController.navigationBarsetTitleTextAttributes:@{NSFontAttributeName:[UIFontfontWithName:@"Zapfino"size:20],NSForegroundColorAttributeName:[[UIColorblueColor]colorWithAlphaComponent:0.78f]}];

#pragma ImageDO

[selfImageLoad];

//[self ImageLoad];

// Do any additional setup after loading the view.

}

-(void)ImageLoad{

self.IM_View= [[UIImageViewalloc]initWithFrame:CGRectMake(20,20,APP_Width-40,APP_Height-104)];

self.IM_View.contentMode=UIViewContentModeScaleAspectFit;

/*

*這樣加載會造成圖片緩存

*? self.IM_View.image = [UIImage imageNamed:@"zsj.jpg"];

*

*? 1)用imageNamed的方式加載時,系統(tǒng)會把圖像Cache到內(nèi)存。如果圖像比較大,或者圖像比較多,用這種方式會消耗很大的內(nèi)存,而且釋放圖像的內(nèi)存是一件相對來說比較麻煩的事情。例如:如果利用imageNamed的方式加載圖像到一個動態(tài)數(shù)組NSMutableArray,然后將將數(shù)組賦予一個UIView的對象的animationImages進行逐幀動畫,那么這將會很有可能造成內(nèi)存泄露。并且釋放圖像所占據(jù)的內(nèi)存也不會那么簡單。但是利用imageNamed加載圖像也有自己的優(yōu)勢。對于同一個圖像系統(tǒng)只會把它Cache到內(nèi)存一次,這對于圖像的重復(fù)利用是非常有優(yōu)勢的。例如:你需要在一個TableView里重復(fù)加載同樣一個圖標,那么用imageNamed加載圖像,系統(tǒng)會把那個圖標Cache到內(nèi)存,在Table里每次利用那個圖像的時候,只會把圖片指針指向同一塊內(nèi)存。這種情況使用imageNamed加載圖像就會變得非常有效。

*/

//self.IM_View.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle]? pathForResource:@"zsj" ofType:@"jpg"]];

/*

*另一種加載方式,不進行圖片的緩存

*

*? ? self.IM_View.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle]? ? pathForResource:@"zsj" ofType:@"jpg"]];

*僅加載圖片,圖像數(shù)據(jù)不會緩存。因此對于較大的圖片以及使用情況較少時,那就可以用該方法,降低內(nèi)存消耗。

*/

/*

* ? self.IM_View.image = [UIImage imageWithData:[NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"zsj" ofType:@"jpg"]]];

*

* ? 3)利用NSData方式加載時,圖像會被系統(tǒng)以數(shù)據(jù)方式加載到程序。當(dāng)你不需要重用該圖像,或者你需要將圖像以數(shù)據(jù)方式存儲到數(shù)據(jù)庫,又或者你要通過網(wǎng)絡(luò)下載一個很大的圖像時,請盡量使用imageWithData的方式加載圖像。

*

*/

/*********************************************************************************/

NSLog(@"W:%f ----H: %f",self.IM_View.image.size.width,self.IM_View.image.size.height);

UIImage* MY = [UIImageimageWithData:[NSDatadataWithContentsOfFile:[[NSBundlemainBundle]pathForResource:@"zsj"ofType:@"jpg"]]scale:0.5];

NSLog(@"M_W:%f ----M_H: %f",MY.size.width,MY.size.height);

/*

*

* scale為0.5的輸出

*

2016-04-14 09:29:00.896 UIImage_處理[880:25088] W:500.000000 ----H: 379.000000

2016-04-14 09:29:00.896 UIImage_處理[880:25088] M_W:1000.000000 ----M_H: 758.000000

*

*

* scale為2的輸出

2016-04-14 09:25:59.338 UIImage_處理[838:23420] W:500.000000 ----H: 379.000000

2016-04-14 09:25:59.338 UIImage_處理[838:23420] M_W:250.000000 ----M_H: 189.500000

*/

/*

*+ (nullable UIImage *)imageWithData:(NSData *)data scale:(CGFloat)scale NS_AVAILABLE_IOS(6_0);

*

*? @scale參數(shù)的說明

*? scale為小于零的數(shù)值(數(shù)值為A)的時候,image的大小(size)是按(1/A)增加的,增加后的大小是:size *(1/A)。

*? scale為大于零的數(shù)值(數(shù)值為A)的時候,image的大小(size)是按(1/A)減小的,減小后的大小是:size *(1/A)。

*/

/*********************************************************************************/

//將圖片轉(zhuǎn)化為二進制流

NSData* ImageData =UIImagePNGRepresentation(MY);

/*

* ? UIImagePNGRepresentation(UIImage * __nonnull image);

* ? return image as PNG. May return nil if image has no CGImageRef or invalid bitmap format

*/

NSData* ImageData1 =UIImageJPEGRepresentation(MY,1.0f);

/*

*? return image as JPEG. May return nil if image has no CGImageRef or invalid bitmap format. compression is 0(most)..1(least)

*/

/*

UIImagePNGRepresentation? |? UIImageJPEGRepresentation兩個的區(qū)別

UIImageJPEGRepresentation函數(shù)需要兩個參數(shù):圖片的引用和壓縮系數(shù).而UIImagePNGRepresentation只需要圖片引用作為參數(shù).通過在實際使用過程中,比較發(fā)現(xiàn): UIImagePNGRepresentation(UIImage* image)要比UIImageJPEGRepresentation(UIImage* image, 1.0)返回的圖片數(shù)據(jù)量大很多.譬如,同樣是讀取攝像頭拍攝的同樣景色的照片, UIImagePNGRepresentation()返回的數(shù)據(jù)量大小為199K ,而UIImageJPEGRepresentation(UIImage* image, 1.0)返回的數(shù)據(jù)量大小只為140KB,比前者少了50多KB.如果對圖片的清晰度要求不高,還可以通過設(shè)置UIImageJPEGRepresentation函數(shù)的第二個參數(shù),大幅度降低圖片數(shù)據(jù)量.譬如,剛才拍攝的圖片,通過調(diào)用UIImageJPEGRepresentation(UIImage* image, 1.0)讀取數(shù)據(jù)時,返回的數(shù)據(jù)大小為140KB,但更改壓縮系數(shù)后,通過調(diào)用UIImageJPEGRepresentation(UIImage* image, 0.5)讀取數(shù)據(jù)時,返回的數(shù)據(jù)大小只有11KB多,大大壓縮了圖片的數(shù)據(jù)量,而且從視角角度看,圖片的質(zhì)量并沒有明顯的降低.因此,在讀取圖片數(shù)據(jù)內(nèi)容時,建議優(yōu)先使用UIImageJPEGRepresentation,并可根據(jù)自己的實際使用場景,設(shè)置壓縮系數(shù),進一步降低圖片數(shù)據(jù)量大小.

*/

/*********************************************************************************/

//將圖片寫入文件

NSData* ImageData_W =UIImageJPEGRepresentation(MY,0.5f);

//獲取系統(tǒng)沙河路徑

NSArray* ArrayPath =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);

NSString* path = [ArrayPathobjectAtIndex:0];

//開始寫入路徑的拼接

NSString* filePath = [pathstringByAppendingString:@"Image.text"];

//寫入

BOOLSucces = ? [ImageData_WwriteToFile:filePathatomically:YES];

NSLog(@"%d",Succes);

/*

輸出:UIImage_處理[1130:41357] 1寫入成功

*/

//將圖片從沙河讀取出來

NSString* ReadPath = filePath;

NSData* ReadData = [NSDatadataWithContentsOfFile:ReadPath];

UIImage* ReadImage = [UIImageimageWithData:ReadDatascale:2];

//self.IM_View.image = ReadImage;

/*********************************************************************************/

//改變圖像的方向

// self.IM_View.image = [UIImage imageWithCGImage:ReadImage.CGImage scale:1.0f orientation:UIImageOrientationLeftMirrored];

/*

*不用旋轉(zhuǎn)控件,來實現(xiàn)圖像的角度旋轉(zhuǎn)

typedef NS_ENUM(NSInteger, UIImageOrientation) {

UIImageOrientationUp,? ? ? ? ? ? // default orientation默認

UIImageOrientationDown,? ? ? ? ? // 180 deg rotation ? ? 180度的轉(zhuǎn)向

UIImageOrientationLeft,? ? ? ? ? // 90 deg CCW圖像逆時針90度

UIImageOrientationRight, ? ? ? ? // 90 deg CW圖像順時針90度

UIImageOrientationUpMirrored,? ? // as above but image mirrored along other axis. horizontal flip圖片的像素中間對稱水平翻轉(zhuǎn)

UIImageOrientationDownMirrored,? // horizontal flip圖片的像素中間對稱垂直翻轉(zhuǎn)

UIImageOrientationLeftMirrored,? // vertical flip圖片的像素左翻轉(zhuǎn)

UIImageOrientationRightMirrored, // vertical flip圖片的像素右翻轉(zhuǎn)

};

*/

/*********************************************************************************/

//用圖像獲取上下文

UIGraphicsBeginImageContext(ReadImage.size);//創(chuàng)建上下文

CGContextRefContextRef =UIGraphicsGetCurrentContext();//獲取上下文對象

CGContextSaveGState(ContextRef);//保存上下文,有利于還原場景

/*

*? [ReadImage drawInRect:CGRectMake(10, 10, 60, 60)];

*是在指定的大小范圍內(nèi)繪制圖片,圖片可完全繪出

*/

/*

*? [ReadImage drawAtPoint:CGPointMake(100, 100)];

*這個是不改變圖片的大小,是更改圖片的起始位置

*/

[ReadImagedrawInRect:CGRectMake(10,10,220,220)blendMode:kCGBlendModeSoftLightalpha:1.0f];

/*

*? - (void)drawInRect:(CGRect)rect blendMode:(CGBlendMode)blendMode alpha:(CGFloat)alpha;

typedef CF_ENUM (int32_t, CGBlendMode) {

kCGBlendModeNormal,正常;也是默認的模式。前景圖會覆蓋背景圖

kCGBlendModeMultiply,正片疊底;混合了前景和背景的顏色,最終顏色比原先的都暗

kCGBlendModeScreen,濾色;把前景和背景圖的顏色先反過來,然后混合

kCGBlendModeOverlay,覆蓋;能保留灰度信息,結(jié)合kCGBlendModeSaturation能保留透明度信息,在imageWithBlendMode方法中兩次執(zhí)行drawInRect方法實現(xiàn)我們基本需求

kCGBlendModeDarken,變暗

kCGBlendModeLighten,變亮

kCGBlendModeColorDodge,顏色變淡

kCGBlendModeColorBurn,顏色加深

kCGBlendModeSoftLight,柔光

kCGBlendModeHardLight,強光

kCGBlendModeDifference,插值

kCGBlendModeExclusion,排除

kCGBlendModeHue,色調(diào)

kCGBlendModeSaturation,飽和度

kCGBlendModeColor,顏色

kCGBlendModeLuminosity,亮度

kCGBlendModeClear,? ? ? ? ? ? ? ? ? ? R = 0清除所有的顏色

kCGBlendModeCopy, ? ? ? ? ? ? ? ? ? ? R = S

kCGBlendModeSourceIn, ? ? ? ? ? ? ? ? R = S*Da

kCGBlendModeSourceOut,? ? ? ? ? ? ? ? R = S*(1 - Da)

kCGBlendModeSourceAtop, ? ? ? ? ? ? ? R = S*Da + D*(1 - Sa)

kCGBlendModeDestinationOver,? ? ? ? ? R = S*(1 - Da) + D

kCGBlendModeDestinationIn,? ? ? ? ? ? R = D*Sa

kCGBlendModeDestinationOut, ? ? ? ? ? R = D*(1 - Sa)

kCGBlendModeDestinationAtop,? ? ? ? ? R = S*(1 - Da) + D*Sa

kCGBlendModeXOR,? ? ? ? ? ? ? ? ? ? ? R = S*(1 - Da) + D*(1 - Sa)

kCGBlendModePlusDarker, ? ? ? ? ? ? ? R = MAX(0, (1 - D) + (1 - S))

kCGBlendModePlusLighter ? ? ? ? ? ? ? R = MIN(1, S + D)? };

*/

//self.IM_View.image = UIGraphicsGetImageFromCurrentImageContext();

CGContextRelease(ContextRef);//移除保存的上下文

UIGraphicsEndImageContext();//結(jié)束圖像上下文

/*********************************************************************************/

//圖片的填充,或者拉伸

//self.IM_View.image = [ReadImage stretchableImageWithLeftCapWidth:50 topCapHeight:50];

/*

*? - (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight? |? - (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets

*這個函數(shù)是UIImage的一個實例函數(shù),它的功能是創(chuàng)建一個內(nèi)容可拉伸,而邊角不拉伸的圖片,需要兩個參數(shù),第一個是不拉伸區(qū)域和左邊框的寬度,第二個參數(shù)是不拉伸區(qū)域和上邊框的寬度。

*/

//? ? self.IM_View.image = [ReadImage resizableImageWithCapInsets:UIEdgeInsetsMake(20, 20, 202, 20)];

self.IM_View.image= [ReadImageimageFlippedForRightToLeftLayoutDirection];

/*********************************************************************************/

//計算圖片的大小

longperMBBytes =1024*1024;

CGImageRefcgimage = [UIImageimageNamed:@"zsj.jpg"].CGImage;

size_tbpp =CGImageGetBitsPerPixel(cgimage);

size_tbpc =CGImageGetBitsPerComponent(cgimage);

size_tbytes_per_pixel = bpp / bpc;

longlPixelsPerMB? = perMBBytes/bytes_per_pixel;

longtotalPixel =CGImageGetWidth([UIImageimageNamed:@"zsj.jpg"].CGImage)*CGImageGetHeight([UIImageimageNamed:@"zsj.jpg"].CGImage);

longtotalFileMB = totalPixel * bpp /lPixelsPerMB;

NSLog(@"%ld",totalFileMB);

/*********************************************************************************/

[self.viewaddSubview:self.IM_View];

}

- (void)didReceiveMemoryWarning {

[superdidReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

/*

#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

// Get the new view controller using [segue destinationViewController].

// Pass the selected object to the new view controller.

}

*/

@end

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容