iOS图片处理,截图,缩放,存储

2024-09-02 01:18

本文主要是介绍iOS图片处理,截图,缩放,存储,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

图片的处理大概分 截图(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); 

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

这篇关于iOS图片处理,截图,缩放,存储的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1128588

相关文章

Python+FFmpeg实现视频自动化处理的完整指南

《Python+FFmpeg实现视频自动化处理的完整指南》本文总结了一套在Python中使用subprocess.run调用FFmpeg进行视频自动化处理的解决方案,涵盖了跨平台硬件加速、中间素材处理... 目录一、 跨平台硬件加速:统一接口设计1. 核心映射逻辑2. python 实现代码二、 中间素材处

Go异常处理、泛型和文件操作实例代码

《Go异常处理、泛型和文件操作实例代码》Go语言的异常处理机制与传统的面向对象语言(如Java、C#)所使用的try-catch结构有所不同,它采用了自己独特的设计理念和方法,:本文主要介绍Go异... 目录一:异常处理常见的异常处理向上抛中断程序恢复程序二:泛型泛型函数泛型结构体泛型切片泛型 map三:文

详解C++ 存储二进制数据容器的几种方法

《详解C++存储二进制数据容器的几种方法》本文主要介绍了详解C++存储二进制数据容器,包括std::vector、std::array、std::string、std::bitset和std::ve... 目录1.std::vector<uint8_t>(最常用)特点:适用场景:示例:2.std::arra

Java使用Spire.Doc for Java实现Word自动化插入图片

《Java使用Spire.DocforJava实现Word自动化插入图片》在日常工作中,Word文档是不可或缺的工具,而图片作为信息传达的重要载体,其在文档中的插入与布局显得尤为关键,下面我们就来... 目录1. Spire.Doc for Java库介绍与安装2. 使用特定的环绕方式插入图片3. 在指定位

SpringSecurity中的跨域问题处理方案

《SpringSecurity中的跨域问题处理方案》本文介绍了跨域资源共享(CORS)技术在JavaEE开发中的应用,详细讲解了CORS的工作原理,包括简单请求和非简单请求的处理方式,本文结合实例代码... 目录1.什么是CORS2.简单请求3.非简单请求4.Spring跨域解决方案4.1.@CrossOr

requests处理token鉴权接口和jsonpath使用方式

《requests处理token鉴权接口和jsonpath使用方式》文章介绍了如何使用requests库进行token鉴权接口的处理,包括登录提取token并保存,还详述了如何使用jsonpath表达... 目录requests处理token鉴权接口和jsonpath使用json数据提取工具总结reques

Python多任务爬虫实现爬取图片和GDP数据

《Python多任务爬虫实现爬取图片和GDP数据》本文主要介绍了基于FastAPI开发Web站点的方法,包括搭建Web服务器、处理图片资源、实现多任务爬虫和数据可视化,同时,还简要介绍了Python爬... 目录一. 基于FastAPI之Web站点开发1. 基于FastAPI搭建Web服务器2. Web服务

C# 空值处理运算符??、?. 及其它常用符号

《C#空值处理运算符??、?.及其它常用符号》本文主要介绍了C#空值处理运算符??、?.及其它常用符号,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面... 目录一、核心运算符:直接解决空值问题1.??空合并运算符2.?.空条件运算符二、辅助运算符:扩展空值处理

浅析Python中如何处理Socket超时

《浅析Python中如何处理Socket超时》在网络编程中,Socket是实现网络通信的基础,本文将深入探讨Python中如何处理Socket超时,并提供完整的代码示例和最佳实践,希望对大家有所帮助... 目录开篇引言核心要点逐一深入讲解每个要点1. 设置Socket超时2. 处理超时异常3. 使用sele

SpringMVC配置、映射与参数处理​入门案例详解

《SpringMVC配置、映射与参数处理​入门案例详解》文章介绍了SpringMVC框架的基本概念和使用方法,包括如何配置和编写Controller、设置请求映射规则、使用RestFul风格、获取请求... 目录1.SpringMVC概述2.入门案例①导入相关依赖②配置web.XML③配置SpringMVC