UIGraphicsBeginImageContext系列知识

UIGraphicsBeginImageContext

创建一个基于位图的上下文(context),并将其设置为当前上下文(context)。方法声明如下:

voidUIGraphicsBeginImageContext(CGSize size);

参数size为新创建的位图上下文的大小。它同时是由UIGraphicsGetImageFromCurrentImageContext函数返回的图形大小。

该函数的功能同UIGraphicsBeginImageContextWithOptions的功能相同,相当与UIGraphicsBeginImageContextWithOptions的opaque参数为NO,scale因子为1.0。

UIGraphicsBeginImageContextWithOptions

函数原型为:

voidUIGraphicsBeginImageContextWithOptions(CGSize size,BOOLopaque, CGFloat scale);

size——同UIGraphicsBeginImageContext

opaque—透明开关,如果图形完全不用透明,设置为YES以优化位图的存储。

scale—–缩放因子

默认创建一个透明的位图上下

UIImageC处理

1、等比缩放

C代码

- (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();

returnscaledImage;

}

2、自定义大小

代码

- (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();

returnreSizeImage;

}

3、处理某个特定的view

只要是继承UIView的object 都可以处理

必须先import QuzrtzCore.framework

代码

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

{

CGRect rect = theView.frame;

UIGraphicsBeginImageContext(rect.size);

CGContextRef context =UIGraphicsGetCurrentContext();

[theView.layer renderInContext:context];

UIImage *img =UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

returnimg;

}

4、存储图片

4.1、存储到app的文件里

把要处理的图片以image.png的名字存储到app

home地下的Document目录中

代码

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

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

4.2、存储到手机的图片库中

代码

CGImageRef screen =

UIGetScreenImage();

UIImage* image = [UIImage

imageWithCGImage:screen];

CGImageRelease(screen);

UIImageWriteToSavedPhotosAlbum(image, self, nil,nil);

获取当前app的名称和版本号

代码

NSDictionary *infoDictionary = [[NSBundle

mainBundle] infoDictionary];

//app名称

NSString *name = [infoDictionaryobjectForKey:@"CFBundleDisplayName"];

//app版本

NSString *version = [infoDictionaryobjectForKey:@"CFBundleShortVersionString"];

// app build版本

NSString *build = [infoDictionaryobjectForKey:@"CFBundleVersion"];

UILabel根据text自动调整大小

代码

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

CGRect frame = label.frame;

frame.size.height = 10000;// 设置一个很大的高度

label.frame = frame;

[label sizeToFit];

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

label.frame = frame;

直接拨打有分机号的电话

代码

[[UIApplication sharedApplication] openURL:[NSURL

URLWithString:@"tel://01011112222,3333"]];

一些有关图像处理的代码片段

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

- (UIImage*)transformWidth:(CGFloat)widthheight:(CGFloat)height; //改变大小

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

+ (UIImage *)imageFromImage:(UIImage *)imageinRect:(CGRect)rect; //裁剪部分图片

+ (void)imageSavedToPhotosAlbum:(UIImage *)image

didFinishSavingWithError:(NSError *)error contextInfo:(void*)contextInfo; //保存图片到媒体库

零)重新设置图片的尺寸

- (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;

}

-)根据给定得图片,从其指定区域截取一张新得图片

-(UIImage *)getImageFromImage{

//大图bigImage

//定义myImageRect,截图的区域

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();

returnresultingImage;

}

三) 捕捉屏幕截图

CALayer实例使用Core

Graphics的renderInContext方法可以将视图绘制到图像上下文中以便转化为其他UIImage实例。前提先#import


+ (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;

}


注:UIGraphicsBeginImageContext(CGSizesize)创建一个基于位图的上下文(context),并将其设置为当前上下文。函数功能与UIGraphicsBeginImageContextWithOptions相同,相当于该方法的opaque参数为NO,scale因子为1.0。而UIGraphicsEndImageContext()方法是移除栈顶的基于当前位图的图形上下文。

视图添加倒影效果


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);  //倒影层框架设置,其中高度是原视图的百分比    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];}

另一:使用Core Graphics创建倒影

+ (CGImageRef) createGradientImage:(CGSize)size{      CGFloat colors[] = {0.0,1.0,1.0,1.0};      //在灰色设备色彩上建立一渐变    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;}

//Create a shrunken frame for the reflection

+ (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];}

关于图片缩放的线程安全和非线程安全操作.

非线程安全的操作只能在主线程中进行操作,对于大图片的处理肯定会消耗大量的时间,如下面的方法

方法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;

}

这种方法的好处是它是线程安全,加上它负责的

(使用正确的颜色空间和位图信息,处理图像方向) 的小东西,UIKit 版本不会。

如何调整和保持长宽比 (如 AspectFill

选项)?

它是非常类似于上述,方法,它看起来像这样:

+ (UIImage*)imageWithImageINCLUDEPICTURE"http://www.61ic.com/Mobile/UploadFiles_9667/201103/20110309123315372.gif"\* MERGEFORMATINET UIImage*)sourceImagescaledToSizeWithSameAspectRatio 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;

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,324评论 5 476
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,303评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,192评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,555评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,569评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,566评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,927评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,583评论 0 257
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,827评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,590评论 2 320
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,669评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,365评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,941评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,928评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,159评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,880评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,399评论 2 342

推荐阅读更多精彩内容