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

简介: <p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"> <span style="font-family:mceinline"><span style="font-size:18px">1.等比率缩放</span><br> - (UIImage *)scaleImage:(

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一下)

  1. CGImageRef screen = UIGetScreenImage();
  2. UIImage* image = [UIImage imageWithCGImage:screen];

2. 对于特定UIView的截屏。

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

  1. -(UIImage*)captureView: (UIView *)theView
  2. {
  3. CGRect rect = theView.frame;
  4. UIGraphicsBeginImageContext(rect.size);
  5. CGContextRef context =UIGraphicsGetCurrentContext();
  6. [theView.layer renderInContext:context];
  7. UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
  8. UIGraphicsEndImageContext();

  9. return img;
  10. }

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

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

  1. UIGraphicsBeginImageContext(CGMakeSize(200,200));
  2. CGContextRefcontext=UIGraphicsGetCurrentContext();
  3. UIGraphicsPushContext(context);
  4. // ...把图写到context中,省略[indent]CGContextBeginPath();
  5. CGContextAddRect(CGMakeRect(0,0,100,100));
  6. CGContextClosePath();[/indent]CGContextDrawPath();
  7. CGContextFlush(); // 强制执行上面定义的操作
  8. UIImage* image = UIGraphicGetImageFromCurrentImageContext();
  9. UIGraphicsPopContext();

4. 存储图像。

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

存储到目录文件是这样

  1. NSString *path = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"image.png"];
  2. [UIImagePNGRepresentation(image) writeToFile:path atomically:YES];

若要存储到图片库里面

  1. UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);


5.  互相转换UImage和CGImage。

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

  1. UIImage* imUI=nil;
  2. CGImageRef imCG=nil;
  3. imUI = [UIImage initWithCGImage:imCG];
  4. imCG = imUI.CGImage;

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

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


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

  1. -(id)invertContrast:(UIImage*)img
  2. {
  3. CGImageRef inImage = img.CGImage; 
  4. CGContextRef ctx;
  5. CFDataRef m_DataRef;
  6. m_DataRef = CGDataProviderCopyData(CGImageGetDataProvider(inImage)); 

  7. int width = CGImageGetWidth( inImage );
  8. int height = CGImageGetHeight( inImage );

  9. int bpc = CGImageGetBitsPerComponent(inImage);
  10. int bpp = CGImageGetBitsPerPixel(inImage);
  11. int bpl = CGImageGetBytesPerRow(inImage);

  12. UInt8 * m_PixelBuf = (UInt8 *) CFDataGetBytePtr(m_DataRef);
  13. int length = CFDataGetLength(m_DataRef);

  14. NSLog(@"len %d", length);
  15. NSLog(@"width=%d, height=%d", width, height);
  16. NSLog(@"1=%d, 2=%d, 3=%d", bpc, bpp,bpl);

  17. for (int index = 0; index < length; index += 4)
  18. m_PixelBuf[index + 0] = 255 - m_PixelBuf[index + 0];// b
  19. m_PixelBuf[index + 1] = 255 - m_PixelBuf[index + 1];// g
  20. m_PixelBuf[index + 2] = 255 - m_PixelBuf[index + 2];// r
  21. }

  22. ctx = CGBitmapContextCreate(m_PixelBuf, width, height, bpb, bpl, CGImageGetColorSpace( inImage ), kCGImageAlphaPremultipliedFirst );
  23. CGImageRef imageRef = CGBitmapContextCreateImage (ctx);
  24. UIImage* rawImage = [UIImage imageWithCGImage:imageRef];
  25. CGContextRelease(ctx);
  26. return rawImage;
  27. }

 

7. 显示图像数据区。

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

  1. CGContextRef ctx = CGBitmapContextCreate(pixelBuf,width,height, bitsPerComponent,bypesPerLine, colorSpace,kCGImageAlphaPremultipliedLast );
  2. CGImageRef imageRef = CGBitmapContextCreateImage (ctx);
  3. UIImage* image = [UIImage imageWithCGImage:imageRef];
  4. NSString* path = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"ss.png"];
  5. [UIImagePNGRepresentation(self.image) writeToFile:path atomically:YES];
  6. CGContextRelease(ctx);
得到图像数据区后就可以很方便的实现图像处理的算法。

完,欢迎指正!

目录
相关文章
|
11月前
|
算法 UED
如何利用体育直播平台进行内容变现
随着互联网的发展,体育赛事的商业化日益多元化,成为推动体育行业发展的关键动力。熊猫比分体育赛事直播平台凭借精准的商业模式和运营策略,在行业中脱颖而出。其直播运营、私域变现和专家推荐等功能,不仅提升了用户体验,还实现了高效变现。通过熊猫比分提供的成熟源码,搭建和运营效率显著提高,助力平台在体育市场中占据一席之地。
394 13
|
网络安全 开发工具 git
解决fatal:remote error:You can’t push to git://github.com/username/*.g
通过上述诊断与修复步骤,绝大多数的推送错误都能得到有效解决,确保您的Git工作流顺畅无阻。
567 1
|
11月前
|
算法
基于GA遗传算法的PID控制器参数优化matlab建模与仿真
本项目基于遗传算法(GA)优化PID控制器参数,通过空间状态方程构建控制对象,自定义GA的选择、交叉、变异过程,以提高PID控制性能。与使用通用GA工具箱相比,此方法更灵活、针对性强。MATLAB2022A环境下测试,展示了GA优化前后PID控制效果的显著差异。核心代码实现了遗传算法的迭代优化过程,最终通过适应度函数评估并选择了最优PID参数,显著提升了系统响应速度和稳定性。
736 15
|
11月前
|
算法 编译器 C语言
【C语言】C++ 和 C 的优缺点是什么?
C 和 C++ 是两种强大的编程语言,各有其优缺点。C 语言以其高效性、底层控制和简洁性广泛应用于系统编程和嵌入式系统。C++ 在 C 语言的基础上引入了面向对象编程、模板编程和丰富的标准库,使其适合开发大型、复杂的软件系统。 在选择使用 C 还是 C++ 时,开发者需要根据项目的需求、语言的特性以及团队的技术栈来做出决策。无论是 C 语言还是 C++,了解其优缺点和适用场景能够帮助开发者在实际开发中做出更明智的选择,从而更好地应对挑战,实现项目目标。
417 0
JAVA并发编程系列(9)CyclicBarrier循环屏障原理分析
本文介绍了拼多多面试中的模拟拼团问题,通过使用 `CyclicBarrier` 实现了多人拼团成功后提交订单并支付的功能。与之前的 `CountDownLatch` 方法不同,`CyclicBarrier` 能够确保所有线程到达屏障点后继续执行,并且屏障可重复使用。文章详细解析了 `CyclicBarrier` 的核心原理及使用方法,并通过代码示例展示了其工作流程。最后,文章还提供了 `CyclicBarrier` 的源码分析,帮助读者深入理解其实现机制。
|
缓存 NoSQL 安全
【Azure Redis 缓存】Azure Redis 4.0 被扫描到漏洞,如何修补呢?
【Azure Redis 缓存】Azure Redis 4.0 被扫描到漏洞,如何修补呢?
242 0
|
JavaScript 前端开发 API
Vue.js 的核心特点
Vue.js 的核心特点
1935 0
AutoJS4.1.0实战教程 ---今日头条极速版
AutoJS4.1.0实战教程 ---今日头条极速版
311 1
|
存储 安全 算法
从系统复杂性看软件架构
一、架构设计是为了解决系统复杂性整个软件技术发展的历史,其实就是一部与“复杂性”斗争的历史。架构也是为了应对软件系统复杂性而提出的一个解决方案,其主要目的是为了解决软件系统复杂性带来的问题。这里包括两个名词:系统和复杂性,下面分别对其进行解析1.1 复杂性的定义复杂性这个名词很复杂,麻省理工学院的物理学家塞思·劳埃德统计了复杂性的定义数量,至少有45种:信息 ,熵 ,算法复杂性 ,算法信息量 ,费
10890 2
从系统复杂性看软件架构
|
DataWorks 关系型数据库 MySQL
在尝试连接到MySQL数据库时遇到了连接超时的问题
在尝试连接到MySQL数据库时遇到了连接超时的问题
1177 4

热门文章

最新文章