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

相关文章

Java 中的 @SneakyThrows 注解使用方法(简化异常处理的利与弊)

《Java中的@SneakyThrows注解使用方法(简化异常处理的利与弊)》为了简化异常处理,Lombok提供了一个强大的注解@SneakyThrows,本文将详细介绍@SneakyThro... 目录1. @SneakyThrows 简介 1.1 什么是 Lombok?2. @SneakyThrows

在 Spring Boot 中实现异常处理最佳实践

《在SpringBoot中实现异常处理最佳实践》本文介绍如何在SpringBoot中实现异常处理,涵盖核心概念、实现方法、与先前查询的集成、性能分析、常见问题和最佳实践,感兴趣的朋友一起看看吧... 目录一、Spring Boot 异常处理的背景与核心概念1.1 为什么需要异常处理?1.2 Spring B

python处理带有时区的日期和时间数据

《python处理带有时区的日期和时间数据》这篇文章主要为大家详细介绍了如何在Python中使用pytz库处理时区信息,包括获取当前UTC时间,转换为特定时区等,有需要的小伙伴可以参考一下... 目录时区基本信息python datetime使用timezonepandas处理时区数据知识延展时区基本信息

Android使用ImageView.ScaleType实现图片的缩放与裁剪功能

《Android使用ImageView.ScaleType实现图片的缩放与裁剪功能》ImageView是最常用的控件之一,它用于展示各种类型的图片,为了能够根据需求调整图片的显示效果,Android提... 目录什么是 ImageView.ScaleType?FIT_XYFIT_STARTFIT_CENTE

关于MongoDB图片URL存储异常问题以及解决

《关于MongoDB图片URL存储异常问题以及解决》:本文主要介绍关于MongoDB图片URL存储异常问题以及解决方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录MongoDB图片URL存储异常问题项目场景问题描述原因分析解决方案预防措施js总结MongoDB图

Python Transformers库(NLP处理库)案例代码讲解

《PythonTransformers库(NLP处理库)案例代码讲解》本文介绍transformers库的全面讲解,包含基础知识、高级用法、案例代码及学习路径,内容经过组织,适合不同阶段的学习者,对... 目录一、基础知识1. Transformers 库简介2. 安装与环境配置3. 快速上手示例二、核心模

一文详解Java异常处理你都了解哪些知识

《一文详解Java异常处理你都了解哪些知识》:本文主要介绍Java异常处理的相关资料,包括异常的分类、捕获和处理异常的语法、常见的异常类型以及自定义异常的实现,文中通过代码介绍的非常详细,需要的朋... 目录前言一、什么是异常二、异常的分类2.1 受检异常2.2 非受检异常三、异常处理的语法3.1 try-

Python使用getopt处理命令行参数示例解析(最佳实践)

《Python使用getopt处理命令行参数示例解析(最佳实践)》getopt模块是Python标准库中一个简单但强大的命令行参数处理工具,它特别适合那些需要快速实现基本命令行参数解析的场景,或者需要... 目录为什么需要处理命令行参数?getopt模块基础实际应用示例与其他参数处理方式的比较常见问http

Java Response返回值的最佳处理方案

《JavaResponse返回值的最佳处理方案》在开发Web应用程序时,我们经常需要通过HTTP请求从服务器获取响应数据,这些数据可以是JSON、XML、甚至是文件,本篇文章将详细解析Java中处理... 目录摘要概述核心问题:关键技术点:源码解析示例 1:使用HttpURLConnection获取Resp

python实现svg图片转换为png和gif

《python实现svg图片转换为png和gif》这篇文章主要为大家详细介绍了python如何实现将svg图片格式转换为png和gif,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录python实现svg图片转换为png和gifpython实现图片格式之间的相互转换延展:基于Py