UIGraphics

UIGraphicsBeginImageContext

創(chuàng)建一個基于位圖的上下文(context),并將其設(shè)置為當(dāng)前上下文(context)。方法聲明如下:

void UIGraphicsBeginImageContext(CGSize size);

參數(shù)size為新創(chuàng)建的位圖上下文的大小蝇恶。它同時是由UIGraphicsGetImageFromCurrentImageContext函數(shù)返回的圖形大小。

該函數(shù)的功能同UIGraphicsBeginImageContextWithOptions的功能相同堤舒,相當(dāng)與UIGraphicsBeginImageContextWithOptions的opaque參數(shù)為NO,scale因子為1.0力试。

UIGraphicsBeginImageContextWithOptions

函數(shù)原型為:

void UIGraphicsBeginImageContextWithOptions(CGSize size, BOOL opaque, CGFloat scale);

size——同UIGraphicsBeginImageContext

opaque—透明開關(guān),如果圖形完全不用透明劣砍,設(shè)置為YES以優(yōu)化位圖的存儲哮幢。

scale—–縮放因子

默認(rèn)創(chuàng)建一個透明的位圖上下文

UIImageC處理

1带膀、等比縮放

C代碼 復(fù)制代碼 收藏代碼

- (UIImage *) scaleImage:(UIImage *)image toScale:(float)scaleSize {

UIGraphicsBeginImageContext(CGSizeMake(image.size.width * scaleSize, image.size.height * scaleSize);

[image drawInRect:CGRectMake(0, 0, image.size.width * scaleSize, image.size.height * scaleSize)];

UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return scaledImage;

}

2、自定義大小

C代碼 復(fù)制代碼 收藏代碼

- (UIImage *) reSizeImage:(UIImage *)image toSize:(CGSize)reSize {

UIGraphicsBeginImageContext(CGSizeMake(reSize.width, reSize.height));

[image drawInRect:CGRectMake(0, 0, reSize.width, reSize.height)];

UIImage *reSizeImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return reSizeImage;

}

3家浇、處理某個特定的view

只要是繼承UIView的object 都可以處理

必須先import QuzrtzCore.framework

C代碼 復(fù)制代碼 收藏代碼

-(UIImage*) captureView:(UIView *)theView {

CGRect rect = theView.frame;

UIGraphicsBeginImageContext(rect.size);

CGContextRef context = UIGraphicsGetCurrentContext();

[theView.layer renderInContext:context];

UIImage *img = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return img;

}

4本砰、存儲圖片

4.1、存儲到app的文件里

把要處理的圖片以image.png的名字存儲到app home地下的Document目錄中

C代碼 復(fù)制代碼 收藏代碼

NSString *path = [[NSHomeDirectory()stringByAppendingPathComponent:@"Documents"]stringByAppendingPathComponent:@"image.png"];

[UIImagePNGRepresentation(image) writeToFile:pathatomically:YES];

4.2钢悲、存儲到手機的圖片庫中

C代碼 復(fù)制代碼 收藏代碼

CGImageRef screen = UIGetScreenImage();

UIImage* image = [UIImage imageWithCGImage:screen];

CGImageRelease(screen);

UIImageWriteToSavedPhotosAlbum(image, self, nil, nil);

獲取當(dāng)前app的名稱和版本號

C代碼 復(fù)制代碼 收藏代碼

NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];

// app名稱

NSString *name = [infoDictionary objectForKey:@"CFBundleDisplayName"];

// app版本

NSString *version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];

// app build版本

NSString *build = [infoDictionary objectForKey:@"CFBundleVersion"];

UILabel根據(jù)text自動調(diào)整大小

C代碼 復(fù)制代碼 收藏代碼

label.text = @"**********";

CGRect frame = label.frame;

frame.size.height = 10000; // 設(shè)置一個很大的高度

label.frame = frame;

[label sizeToFit];

frame.size.height = label.frame.size.height;

label.frame = frame;

直接撥打有分機號的電話

C代碼 復(fù)制代碼 收藏代碼

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://01011112222,3333"]];

一些有關(guān)圖像處理的代碼片段

- (UIImage *)rescaleImage:(UIImage *)img ToSize:(CGSize)size; //圖片縮放裁剪

- (UIImage*)transformWidth:(CGFloat)width height:(CGFloat)height; //改變大小

+ (UIImage *)addImage:(UIImage *)image1 toImage:(UIImage *)image2; //合并圖片

+ (UIImage *)imageFromImage:(UIImage *)image inRect:(CGRect)rect; //裁剪部分圖片

+ (void)imageSavedToPhotosAlbum:(UIImage *)image

didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo; //保存圖片到媒體庫

零)重新設(shè)置圖片的尺寸

- (UIImage *)rescaleImage:(UIImage *)img ToSize:(CGSize)size {

CGRect rect = CGRectMake(0.0, 0.0, size.width, size.height);

UIGraphicsBeginImageContext(rect.size);

[img drawInRect:rect]; // scales image to rect

UIImage *resImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return resImage;

}

-)根據(jù)給定得圖片点额,從其指定區(qū)域截取一張新得圖片

-(UIImage *)getImageFromImage{

//大圖bigImage

//定義myImageRect,截圖的區(qū)域

CGRect myImageRect = CGRectMake(10.0, 10.0, 57.0, 57.0);

UIImage* bigImage= [UIImage imageNamed:@"k00030.jpg"];

CGImageRef imageRef = bigImage.CGImage;

CGImageRef subImageRef = CGImageCreateWithImageInRect(imageRef, myImageRect);

CGSize size;

size.width = 57.0;

size.height = 57.0;

UIGraphicsBeginImageContext(size);

CGContextRef context = UIGraphicsGetCurrentContext();

CGContextDrawImage(context, myImageRect, subImageRef);

UIImage* smallImage = [UIImage imageWithCGImage:subImageRef];

UIGraphicsEndImageContext();

return smallImage;

}

二) 合并兩張圖片

- (UIImage *)addImage:(UIImage *)image1 toImage:(UIImage *)image2 {

UIGraphicsBeginImageContext(image1.size);

// Draw image1

[image1 drawInRect:CGRectMake(0, 0, image1.size.width, image1.size.height)];

// Draw image2

[image2 drawInRect:CGRectMake(0, 0, image2.size.width, image2.size.height)];

UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return resultingImage;

}

三) 捕捉屏幕截圖

CALayer實例使用Core Graphics的renderInContext方法可以將視圖繪制到圖像上下文中以便轉(zhuǎn)化為其他UIImage實例莺琳。前提先#import

復(fù)制代碼

+ (UIImage *) imageFromView: (UIView *)theView { // draw a view's contents into an image context UIGraphicsBeginImageContext(theView.frame.size); CGContextRef context = UIGraphicsGetCurrentContext(); [theView.layer renderInContext:context]; UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return theImage; }

復(fù)制代碼

注:UIGraphicsBeginImageContext(CGSize size)創(chuàng)建一個基于位圖的上下文(context)还棱,并將其設(shè)置為當(dāng)前上下文。函數(shù)功能與UIGraphicsBeginImageContextWithOptions相同惭等,相當(dāng)于該方法的opaque參數(shù)為NO珍手,scale因子為1.0。而UIGraphicsEndImageContext()方法是移除棧頂?shù)幕诋?dāng)前位圖的圖形上下文辞做。

視圖添加倒影效果

復(fù)制代碼

const CGFloat kReflectPercent = -0.25f; const CGFloat kReflectOpacity = 0.3f; const CGFloat kReflectDistance = 10.0f; + (void)addSimpleReflectionToView:(UIView *)theView { CALayer *reflectionLayer = [CALayer layer]; reflectionLayer.contents = [theView layer].contents; reflectionLayer.opacity = kReflectOpacity; reflectionLayer.frame = CGRectMake(0.0f,0.0f,theView.frame.size.width,theView.frame.size.height*kReflectPercent); //倒影層框架設(shè)置琳要,其中高度是原視圖的百分比 CATransform3D stransform = CATransform3DMakeScale(1.0f,-1.0f,1.0f); CATransform3D transform = CATransform3DTranslate(stransform,0.0f,-(kReflectDistance + theView.frame.size.height),0.0f); reflectionLayer.transform = transform; reflectionLayer.sublayerTransform = reflectionLayer.transform; [[theView layer] addSublayer:reflectionLayer]; }

復(fù)制代碼

另一:使用Core Graphics創(chuàng)建倒影

復(fù)制代碼

+ (CGImageRef) createGradientImage:(CGSize)size { CGFloat colors[] = {0.0,1.0,1.0,1.0}; //在灰色設(shè)備色彩上建立一漸變 CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray(); CGContextRef context = CGBitmapContextCreate(nil,size.width,size.height,8,0,colorSpace,kCGImageAlphaNone); CGGradientRef gradient = CGGradientCreateWithColorComponents(colorSpace,colors,NULL,2); CGColorSpaceRelease(colorSpace); //繪制線性漸變 CGPoint p1 = CGPointZero; CGPoint p2 = CGPointMake(0,size.height); CGContextDrawLinearGradient(context,gradient,p1,p2,kCGGradientDrawsAfterEndLocation); //Return the CGImage CGImageRef theCGImage = CGBitmapContextCreateImage(context); CFRelease(gradient); CGContextRelease(context); return theCGImage; }

復(fù)制代碼

//Create a shrunken frame for the reflection

復(fù)制代碼

+ (UIImage *) reflectionOfView:(UIView *)theView WithPercent:(CGFloat) percent { //Retain the width but shrink the height CGSize size = CGSizeMake(theView.frame.size.width, theView.frame.size.height * percent); //Shrink the View UIGraphicsBeginImageContext(size); CGContextRef context = UIGraphicsGetCurrentContext(); [theView.layer renderInContext:context]; UIImage *partialimg = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); //build the mask CGImageRef mask = [ImageHelper createGradientImage:size]; CGImageRef ref = CGImageCreateWithMask(partialimg.CGImage,mask); UIImage *theImage = [UIImage imageWithCGImage:ref]; CGImageRelease(ref); CGImageRelease(mask); return theImage; } const CGFloat kReflectDistance = 10.0f; + (void) addReflectionToView: (UIView *)theView { theView.clipsToBounds = NO; UIImageView *reflection = [[UIImageView alloc] initWithImage:[ImageHelper reflectionOfView:theView withPercent:0.45f]]; CGRect frame = reflection.frame; frame.origin = CGPointMake(0.0f, theView.frame.size.height + kReflectDistance); reflection.frame = frame; // add the reflection as a simple subview [theView addSubView:reflection]; [reflection release]; }

關(guān)于圖片縮放的線程安全和非線程安全操作.

非線程安全的操作只能在主線程中進行操作,對于大圖片的處理肯定會消耗大量的時間,如下面的方法

方法 1: 使用 UIKit

+ (UIImage*)imageWithImage INCLUDEPICTURE "http://www.61ic.com/Mobile/UploadFiles_9667/201103/20110309123315372.gif" \* MERGEFORMATINET UIImage*)image scaledToSize INCLUDEPICTURE "http://www.61ic.com/Mobile/UploadFiles_9667/201103/20110309123315372.gif" \* MERGEFORMATINET CGSize)newSize;

{

// Create a graphics image context

UIGraphicsBeginImageContext(newSize);

// Tell the old image to draw in this new context, with the desired

// new size

[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];

// Get the new image from the context

UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();

// End the context

UIGraphicsEndImageContext();

// Return the new image.

return newImage;

}

此方法很簡單, 但是秤茅,這種方法不是線程安全的情況下.

方法 2: 使用 CoreGraphics

+ (UIImage*)imageWithImage INCLUDEPICTURE "http://www.61ic.com/Mobile/UploadFiles_9667/201103/20110309123315372.gif" \* MERGEFORMATINET UIImage*)sourceImage scaledToSize INCLUDEPICTURE "http://www.61ic.com/Mobile/UploadFiles_9667/201103/20110309123315372.gif" \* MERGEFORMATINET CGSize)newSize;

{

CGFloat targetWidth = targetSize.width;

CGFloat targetHeight = targetSize.height;

CGImageRef imageRef = [sourceImage CGImage];

CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(imageRef);

CGColorSpaceRef colorSpaceInfo = CGImageGetColorSpace(imageRef);

if (bitmapInfo == kCGImageAlphaNone) {

bitmapInfo = kCGImageAlphaNoneSkipLast;

}

CGContextRef bitmap;

if (sourceImage.imageOrientation == UIImageOrientationUp ||sourceImage.imageOrientation == UIImageOrientationDown) {

bitmap = CGBitmapContextCreate(NULL, targetWidth, targetHeight,CGImageGetBitsPerComponent(imageRef),CGImageGetBytesPerRow(imageRef), colorSpaceInfo, bitmapInfo);

} else {

bitmap = CGBitmapContextCreate(NULL, targetHeight, targetWidth,CGImageGetBitsPerComponent(imageRef),CGImageGetBytesPerRow(imageRef), colorSpaceInfo, bitmapInfo);

}

if (sourceImage.imageOrientation == UIImageOrientationLeft) {

CGContextRotateCTM (bitmap, radians(90));

CGContextTranslateCTM (bitmap, 0, -targetHeight);

} else if (sourceImage.imageOrientation ==UIImageOrientationRight) {

CGContextRotateCTM (bitmap, radians(-90));

CGContextTranslateCTM (bitmap, -targetWidth, 0);

} else if (sourceImage.imageOrientation == UIImageOrientationUp) {

// NOTHING

} else if (sourceImage.imageOrientation == UIImageOrientationDown){

CGContextTranslateCTM (bitmap, targetWidth, targetHeight);

CGContextRotateCTM (bitmap, radians(-180.));

}

CGContextDrawImage(bitmap, CGRectMake(0, 0, targetWidth,targetHeight), imageRef);

CGImageRef ref = CGBitmapContextCreateImage(bitmap);

UIImage* newImage = [UIImage imageWithCGImage:ref];

CGContextRelease(bitmap);

CGImageRelease(ref);

return newImage;

}

這種方法的好處是它是線程安全稚补,加上它負(fù)責(zé)的 (使用正確的顏色空間和位圖信息,處理圖像方向) 的小東西框喳,UIKit 版本不會课幕。

如何調(diào)整和保持長寬比 (如 AspectFill 選項)厦坛?

它是非常類似于上述,方法乍惊,它看起來像這樣:

+ (UIImage*)imageWithImage INCLUDEPICTURE "http://www.61ic.com/Mobile/UploadFiles_9667/201103/20110309123315372.gif" \* MERGEFORMATINET UIImage*)sourceImage scaledToSizeWithSameAspectRatio INCLUDEPICTURE "http://www.61ic.com/Mobile/UploadFiles_9667/201103/20110309123315372.gif" \* MERGEFORMATINET CGSize)targetSize;

{

CGSize imageSize = sourceImage.size;

CGFloat width = imageSize.width;

CGFloat height = imageSize.height;

CGFloat targetWidth = targetSize.width;

CGFloat targetHeight = targetSize.height;

CGFloat scaleFactor = 0.0;

CGFloat scaledWidth = targetWidth;

CGFloat scaledHeight = targetHeight;

CGPoint thumbnailPoint = CGPointMake(0.0,0.0);

if (CGSizeEqualToSize(imageSize, targetSize) == NO) {

CGFloat widthFactor = targetWidth / width;

CGFloat heightFactor = targetHeight / height;

if (widthFactor > heightFactor) {

scaleFactor = widthFactor; // scale to fit height

}

else {

scaleFactor = heightFactor; // scale to fit width

}

scaledWidth = width * scaleFactor;

scaledHeight = height * scaleFactor;

// center the image

if (widthFactor > heightFactor) {

thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;

}

else if (widthFactor < heightFactor) {

thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;

}

}

CGImageRef imageRef = [sourceImage CGImage];

CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(imageRef);

CGColorSpaceRef colorSpaceInfo = CGImageGetColorSpace(imageRef);

if (bitmapInfo == kCGImageAlphaNone) {

bitmapInfo = kCGImageAlphaNoneSkipLast;

}

CGContextRef bitmap;

if (sourceImage.imageOrientation == UIImageOrientationUp ||sourceImage.imageOrientation == UIImageOrientationDown) {

bitmap = CGBitmapContextCreate(NULL, targetWidth, targetHeight,CGImageGetBitsPerComponent(imageRef),CGImageGetBytesPerRow(imageRef), colorSpaceInfo, bitmapInfo);

} else {

bitmap = CGBitmapContextCreate(NULL, targetHeight, targetWidth,CGImageGetBitsPerComponent(imageRef),CGImageGetBytesPerRow(imageRef), colorSpaceInfo, bitmapInfo);

}

// In the right or left cases, we need to switch scaledWidth and scaledHeight,

// and also the thumbnail point

if (sourceImage.imageOrientation == UIImageOrientationLeft) {

thumbnailPoint = CGPointMake(thumbnailPoint.y, thumbnailPoint.x);

CGFloat oldScaledWidth = scaledWidth;

scaledWidth = scaledHeight;

scaledHeight = oldScaledWidth;

CGContextRotateCTM (bitmap, radians(90));

CGContextTranslateCTM (bitmap, 0, -targetHeight);

} else if (sourceImage.imageOrientation ==UIImageOrientationRight) {

thumbnailPoint = CGPointMake(thumbnailPoint.y, thumbnailPoint.x);

CGFloat oldScaledWidth = scaledWidth;

scaledWidth = scaledHeight;

scaledHeight = oldScaledWidth;

CGContextRotateCTM (bitmap, radians(-90));

CGContextTranslateCTM (bitmap, -targetWidth, 0);

} else if (sourceImage.imageOrientation == UIImageOrientationUp) {

// NOTHING

} else if (sourceImage.imageOrientation == UIImageOrientationDown){

CGContextTranslateCTM (bitmap, targetWidth, targetHeight);

CGContextRotateCTM (bitmap, radians(-180.));

}

CGContextDrawImage(bitmap, CGRectMake(thumbnailPoint.x,thumbnailPoint.y, scaledWidth, scaledHeight), imageRef);

CGImageRef ref = CGBitmapContextCreateImage(bitmap);

UIImage* newImage = [UIImage imageWithCGImage:ref];

CGContextRelease(bitmap);

CGImageRelease(ref);

return newImage;

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末杜秸,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子润绎,更是在濱河造成了極大的恐慌撬碟,老刑警劉巖,帶你破解...
    沈念sama閱讀 207,113評論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件凡橱,死亡現(xiàn)場離奇詭異小作,居然都是意外死亡亭姥,警方通過查閱死者的電腦和手機稼钩,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,644評論 2 381
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來达罗,“玉大人坝撑,你說我怎么就攤上這事×溉啵” “怎么了巡李?”我有些...
    開封第一講書人閱讀 153,340評論 0 344
  • 文/不壞的土叔 我叫張陵,是天一觀的道長扶认。 經(jīng)常有香客問我侨拦,道長,這世上最難降的妖魔是什么辐宾? 我笑而不...
    開封第一講書人閱讀 55,449評論 1 279
  • 正文 為了忘掉前任狱从,我火速辦了婚禮,結(jié)果婚禮上叠纹,老公的妹妹穿的比我還像新娘季研。我一直安慰自己,他們只是感情好誉察,可當(dāng)我...
    茶點故事閱讀 64,445評論 5 374
  • 文/花漫 我一把揭開白布与涡。 她就那樣靜靜地躺著,像睡著了一般持偏。 火紅的嫁衣襯著肌膚如雪驼卖。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,166評論 1 284
  • 那天鸿秆,我揣著相機與錄音酌畜,去河邊找鬼。 笑死谬莹,一個胖子當(dāng)著我的面吹牛檩奠,可吹牛的內(nèi)容都是我干的桩了。 我是一名探鬼主播,決...
    沈念sama閱讀 38,442評論 3 401
  • 文/蒼蘭香墨 我猛地睜開眼埠戳,長吁一口氣:“原來是場噩夢啊……” “哼井誉!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起整胃,我...
    開封第一講書人閱讀 37,105評論 0 261
  • 序言:老撾萬榮一對情侶失蹤颗圣,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后屁使,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體在岂,經(jīng)...
    沈念sama閱讀 43,601評論 1 300
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 36,066評論 2 325
  • 正文 我和宋清朗相戀三年蛮寂,在試婚紗的時候發(fā)現(xiàn)自己被綠了蔽午。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 38,161評論 1 334
  • 序言:一個原本活蹦亂跳的男人離奇死亡酬蹋,死狀恐怖及老,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情范抓,我是刑警寧澤骄恶,帶...
    沈念sama閱讀 33,792評論 4 323
  • 正文 年R本政府宣布,位于F島的核電站匕垫,受9級特大地震影響僧鲁,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜象泵,卻給世界環(huán)境...
    茶點故事閱讀 39,351評論 3 307
  • 文/蒙蒙 一寞秃、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧单芜,春花似錦蜕该、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,352評論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至扒腕,卻和暖如春绢淀,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背瘾腰。 一陣腳步聲響...
    開封第一講書人閱讀 31,584評論 1 261
  • 我被黑心中介騙來泰國打工皆的, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人蹋盆。 一個月前我還...
    沈念sama閱讀 45,618評論 2 355
  • 正文 我出身青樓费薄,卻偏偏與公主長得像硝全,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子楞抡,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 42,916評論 2 344

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