UIImage 图片处理:截图,缩放,设定大小,存储

图片的处理大概分 截图(capture),  缩放(scale), 设定大小(resize),  存储(save)

1.等比率缩放

- (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.自定长宽

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

-(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.储存图片

储存图片这里分成储存到app的文件里和储存到手机的图片库里

1) 储存到app的文件里

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

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

把要处理的图片, 以image.png名称存到app home下的Documents目录里

2)储存到手机的图片库里(必须在真机使用,模拟器无法使用)

CGImageRef screen = UIGetScreenImage();

UIImage* image = [UIImage imageWithCGImage:screen];

CGImageRelease(screen);

UIImageWriteToSavedPhotosAlbum(image, self, nil, nil);

UIGetScreenImage(); // 原来是private(私有)api, 用来截取整个画面,不过SDK 4.0后apple就开放了



以下代码用到了Quartz Framework 和 Core Graphics Framework. 在workspace的framework目录里添加这两个framework.在UIKit里,图像类UIImage和CGImageRef的画图操作都是通过Graphics Context来完成。Graphics Context封装了变换的参数,使得在不同的坐标系里操作图像非常方便。缺点就是,获取图像的数据不是那么方便。下面会给出获取数据区的代码。

1. 从UIView中获取图像相当于窗口截屏。

(ios提供全局的全屏截屏函数UIGetScreenView(). 如果需要特定区域的图像,可以crop一下)

CGImageRef screen = UIGetScreenImage();

UIImage* image = [UIImage imageWithCGImage:screen];

2. 对于特定UIView的截屏。

(可以把当前View的layer,输出到一个ImageContext中,然后利用这个ImageContext得到UIImage)

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

{

CGRect rect = theView.frame;

UIGraphicsBeginImageContext(rect.size);

CGContextRef context =UIGraphicsGetCurrentContext();

[theView.layer renderInContext:context];

UIImage *img = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return img;

}

3. 如果需要裁剪指定区域。

(可以path & clip,以下例子是建一个200x200的图像上下文,再截取出左上角)

UIGraphicsBeginImageContext(CGMakeSize(200,200));

CGContextRefcontext=UIGraphicsGetCurrentContext();

UIGraphicsPushContext(context);

// ...把图写到context中,省略[indent]CGContextBeginPath();

CGContextAddRect(CGMakeRect(0,0,100,100));

CGContextClosePath();[/indent]CGContextDrawPath();

CGContextFlush();// 强制执行上面定义的操作

UIImage* image = UIGraphicGetImageFromCurrentImageContext();

UIGraphicsPopContext();

4. 存储图像。

(分别存储到home目录文件和图片库文件。)

存储到目录文件是这样

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

[UIImagePNGRepresentation(image) writeToFile:path atomically:YES];

若要存储到图片库里面

UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);

5.互相转换UImage和CGImage。

(UImage封装了CGImage, 互相转换很容易)

UIImage* imUI=nil;

CGImageRef imCG=nil;

imUI = [UIImage initWithCGImage:imCG];

imCG = imUI.CGImage;

6. 从CGImage上获取图像数据区。

(在apple dev上有QA, 不过好像还不支持ios)

下面给出一个在ios上反色的例子

-(id)invertContrast:(UIImage*)img

{

CGImageRef inImage = img.CGImage;

CGContextRef ctx;

CFDataRef m_DataRef;

m_DataRef = CGDataProviderCopyData(CGImageGetDataProvider(inImage));

int width = CGImageGetWidth( inImage );

int height = CGImageGetHeight( inImage );

int bpc = CGImageGetBitsPerComponent(inImage);

int bpp = CGImageGetBitsPerPixel(inImage);

int bpl = CGImageGetBytesPerRow(inImage);

UInt8 * m_PixelBuf = (UInt8 *) CFDataGetBytePtr(m_DataRef);

int length = CFDataGetLength(m_DataRef);

NSLog(@"len %d", length);

NSLog(@"width=%d, height=%d", width, height);

NSLog(@"1=%d, 2=%d, 3=%d", bpc, bpp,bpl);

for (int index = 0; index < length; index += 4)

{

m_PixelBuf[index + 0] = 255 - m_PixelBuf[index + 0];// b

m_PixelBuf[index + 1] = 255 - m_PixelBuf[index + 1];// g

m_PixelBuf[index + 2] = 255 - m_PixelBuf[index + 2];// r

}

ctx = CGBitmapContextCreate(m_PixelBuf, width, height, bpb, bpl, CGImageGetColorSpace( inImage ), kCGImageAlphaPremultipliedFirst );

CGImageRef imageRef = CGBitmapContextCreateImage (ctx);

UIImage* rawImage = [UIImage imageWithCGImage:imageRef];

CGContextRelease(ctx);

return rawImage;

}

7.显示图像数据区。

(显示图像数据区,也就是unsigned char*转为graphics context或者UIImage或和CGImageRef)

CGContextRef ctx = CGBitmapContextCreate(pixelBuf,width,height, bitsPerComponent,bypesPerLine, colorSpace,kCGImageAlphaPremultipliedLast );

CGImageRef imageRef = CGBitmapContextCreateImage (ctx);

UIImage* image = [UIImage imageWithCGImage:imageRef];

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

[UIImagePNGRepresentation(self.image) writeToFile:path atomically:YES];

CGContextRelease(ctx);

得到图像数据区后就可以很方便的实现图像处理的算法。

转自别处,用于自己笔记之用。侵删

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

推荐阅读更多精彩内容