UIImage+Extension

/// 获取后的数据 a.length > b.length. 同时,使用UIIMageJPEGRepresnetation压缩图片,如果compressionQuality设置为1.0,得到的图片大小比原图大,测试之后,压缩比在0.7--0.8之间大致为原图的大小
NSData *a = UIImagePNGRepresentation(UIImage *image)
NSData *b = UIIMageJPEGRepresnetation(UIImage * image, CGFloat compressionQuality)

1. 裁剪图片上的某一部分

/// 获取指定部分的图片
- (UIImage *)FF_AcquireSpecialSizeImage:(CGRect)rect {CGImageRef imageRef = [self CGImage];CGImageRef needImageRef = CGImageCreateWithImageInRect(imageRef, rect);return [UIImage imageWithCGImage:needImageRef];
}

2.将图片重画在某一部分,会压缩图片的质量

/// 重画图片到指定的Size
- (UIImage *)FF_CompressSize:(CGSize)size {UIImage *result = self;UIGraphicsBeginImageContext(size);[result drawInRect:CGRectMake(0, 0, size.width, size.height)];result = UIGraphicsGetImageFromCurrentImageContext();UIGraphicsEndImageContext();return result;
}

3.将图片压缩到指定大小,压缩质量

/**将图片压缩到指定的质量@param kb 1MB == 1024KB@return 图片压缩后的质量使用二分法提高效率,一般通过6次,可以达到指定的大小,也有例外,之后通过缩小尺寸来实现目标*/
- (NSData *)FF_CompressQualityToSpecialKB:(CGFloat)kb {CGFloat specialBytes = kb * 1024;NSData *imageDate = UIImageJPEGRepresentation(self, 1.0);if (imageDate.length < specialBytes) {return imageDate;}CGFloat min = 0;CGFloat max = 1;CGFloat compress = 1;for (int i = 0; i < 6; i++) {compress = (max + min) / 2;imageDate = UIImageJPEGRepresentation(self, compress);if (imageDate.length < specialBytes * 0.9) {min = compress;}else if (imageDate.length > specialBytes) {max = compress;}else {break;}}if (imageDate.length <= specialBytes) {return imageDate;}UIImage *tempImage = [UIImage imageWithData:imageDate];NSUInteger lastDateLength = 0;while (imageDate.length > specialBytes && lastDateLength != imageDate.length) {lastDateLength = imageDate.length;CGFloat tempScale = specialBytes / imageDate.length;CGSize tempSize = CGSizeMake((NSUInteger)(tempImage.size.width * tempScale),(NSUInteger)(tempImage.size.height * tempScale));UIGraphicsBeginImageContext(tempSize);[tempImage drawInRect:CGRectMake(0, 0, tempSize.width, tempSize.height)];tempImage = UIGraphicsGetImageFromCurrentImageContext();UIGraphicsEndImageContext();imageDate = UIImageJPEGRepresentation(tempImage, compress);}return imageDate;
}

4 通过颜色获取图片

+ (UIImage *)FF_AcquireImageFromColor:(UIColor *)color {UIGraphicsBeginImageContext(CGSizeMake(1, 1));CGContextRef ref = UIGraphicsGetCurrentContext();CGContextSetFillColorWithColor(ref, color.CGColor);CGContextFillRect(ref, CGRectMake(0, 0, 1, 1));UIImage *img = UIGraphicsGetImageFromCurrentImageContext();UIGraphicsEndImageContext();return img;
}

5. 创建自定义的图库,并保存图片到自定义相册

/*
1. 导入框架 <Photo/Photo.h>
2. PHAsssetColoction 图库
3. PHAsset                图片集合
4. PHPhotoLibrary      在Library中进行各种操作,创建图库,图片等
5. PHAssetCollectionChangeRequest 创建PHAssetCollection
6. PHAssetChangeRequest   创建PHAsset
*/- (PHAssetCollection *)FF_CreatePhotoLibrary {NSString *title = [[NSBundle mainBundle].infoDictionary objectForKey:(NSString *)kCFBundleNameKey];PHFetchResult *result = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];PHAssetCollection *needCollection = nil;for (PHAssetCollection *collection in result) {if ([collection.localizedTitle isEqualToString:title]) {needCollection = collection;}}__block NSString *localIdentifierId = nil;if (!needCollection) {[[PHPhotoLibrary sharedPhotoLibrary] performChangesAndWait:^{localIdentifierId = [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:title].placeholderForCreatedAssetCollection.localIdentifier;} error:nil];}if (localIdentifierId) {needCollection = [PHAssetCollection fetchAssetCollectionsWithLocalIdentifiers:@[localIdentifierId] options:nil].firstObject;}return needCollection;
}/// 保存图片到相机相册
- (PHFetchResult<PHAsset *> *)FF_CreatedAssets {__block NSString *localIdentital = nil;[[PHPhotoLibrary sharedPhotoLibrary] performChangesAndWait:^{localIdentital = [PHAssetChangeRequest creationRequestForAssetFromImage:self].placeholderForCreatedAsset.localIdentifier;} error:nil];if (localIdentital) {return [PHAsset fetchAssetsWithLocalIdentifiers:@[localIdentital] options:nil];}else {return nil;}
}/// 保存到自定义相册
- (void)FF_SaveImageToAlbum {if (![self FF_PhotoLibraryAuthorization]) {return;}PHAssetCollection *collection = [self FF_CreatePhotoLibrary];PHFetchResult<PHAsset *> *result = [self FF_CreatedAssets];[[PHPhotoLibrary sharedPhotoLibrary] performChangesAndWait:^{PHAssetCollectionChangeRequest *request = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:collection];[request insertAssets:result atIndexes:[NSIndexSet indexSetWithIndex:0]];} error:nil];
}

转载于:https://www.cnblogs.com/jisa/p/10451446.html

iOS-项目开发1-UIImage相关推荐

  1. 第一个 iOS 项目开发小结 - SwiftUI 学习资料、开源项目

    第一个 iOS 项目开发小结 SwiftUI 知识点小记 学习资料 开源项目 一些问题记录 项目预览 这段时间由于项目需要,我学习了一段时间 SwiftUI 并单独完成了一个系统的开发,耗时两个星期, ...

  2. iOS 项目开发总览,开发者账号

    一.iOS 项目开发总览 1.iOS项目开发通常包括哪些环节? 从整体来说,iOS项目开发通常包括产品设计.开发.测试.上架几个阶段.从具体流程来讲,可分为开发者帐号申请.模拟器开发.真机开发.真机测 ...

  3. iOS项目开发实战——设置视图的透明度改变动画

    在iOS中 ,透明度的改变可以让View视图以一种渐变的效果动态的出现或者消退,非常有意思.这里我们将会对一个View控件的色块执行透明度改变的动画.关于其他的动画效果可以参考我的其他几篇博客< ...

  4. iOS项目开发优秀文章汇总

    UI界面 iOS和Android 界面设计尺寸规范  http://www.alibuybuy.com/posts/85486.html iPhone app界面设计尺寸规范  http://www. ...

  5. iOS 项目开发工具JIRA

    1. 项目管理体系平台 : JIRA 工作中为了有效管控项目开发进度与责任问题,引入了 jira 管理平台 目的: 任务明晰 与 责任人挂钩 方便管控进度 有利于bug的消除 各部门配合方便 使用注意 ...

  6. iOS项目开发实战——iOS网络编程获取网页Html源代码

    如今我们身处互联网的时代,任何一个软件或是App,都会或多或少与网络打交道,并不断发生数据交互.一个没有涉及网络编程的应用会显得比较low,这里我们将会开始使用Swift开发iOS应用,并且主要来实现 ...

  7. iOS项目开发中Git的使用

    一.Git介绍 Git是一个项目源码管理系统,在多人合作开发过程中是至关重要的.在项目开发中,我们可以通过Git客户端(Github.Tower.Tortoise等)或者通过命令行来使用Git,关于G ...

  8. iOS项目开发实战——开发基于Handoff的App(Swift)

    iOS8推出一个新特性,叫做Handoff.Handoff中文含义为换手(把接力棒传给下一个人),可以在一台Mac和iOS设备上开始工作,中途将工作交换到另一个Mac或iOS设备中进行.这个在iOS8 ...

  9. iOS项目开发实战——使用Xcode6设计自己定义控件与图形

    在iOS开发中,有很多控件都是Xcode默认提供的.使用这些控件是很方便的.可是因为某些须要.须要自己设计控件,那么应该怎么做呢?在Xcode6中提供了这种接口,同意开发人员高速开发自己定义控件,而且 ...

  10. iOS项目开发— CoreLocation的定位服务和地理编码与发编码实现

      一.CoreLocation简介 1.在移动互联网时代,移动app能解决用户的很多生活琐事,比如 (1)导航:去任意陌生的地方 (2)周边:找餐馆.找酒店.找银行.找电影院 2.在上述应用中,都用 ...

最新文章

  1. 探讨ASP.NET AJAX客户端开发技术
  2. Windows下安装Zabbix agent
  3. 【鉴轻尘】BTC的暴跌的是因为BCH的分叉,但是你知道为什么?
  4. ImportError: libclntsh.so.11.1: cannot open shared object file: No such file or directory解决方案
  5. Npm常用命令有哪些
  6. kafka报错:Error writing out kafka.log:type=Log,name=LogStartOffset,topic=xx EofException
  7. 2 Linux磁盘管理
  8. 中国民间秘术大揭露【实用】
  9. Learn RxJava
  10. 任意版本nodejs下载
  11. IT管理员喜欢OpManager的十大原因
  12. Review board 和 Git 配合使用 一
  13. C51单片机 SIM900A 手机给门禁解锁 智能家居 【C程序代码已开源,更新至Ver4.0】...
  14. 乐优商城之品牌新增fastDFS(九)
  15. python程序控制结构实验报告_20193227 实验二《Python程序设计》实验报告
  16. 虚拟硬盘 服务器 破解,服务器版虚拟硬盘_Primo Ramdisk Server Edition V5.6.1 免费版
  17. 无极性电容和有极性电容的爆炸
  18. 小恐龙游戏挑战:第11天-简单画好小恐龙并上色
  19. 重启计算机桌面出现system,win10系统电脑重启后桌面图标顺序乱了的解决方法
  20. 共话机器翻译新风向,第二届小牛翻译论坛启幕在即

热门文章

  1. 用JS实现简单的省市联动
  2. 生意场逃不开三个关键词:留存、转化、数据
  3. 初创公司要严控会议时长,日会5-15分钟,周会不超30分钟
  4. 黄奕:我之前弄了一个母婴网站,根本就不会做生意,几个月上千万就赔进去了
  5. 满足ThreadLocal目的
  6. 专卖店荣耀magicbookpro预装系统是Linux,换商家送的U盘里win10系统有影响吗?
  7. Js判断是否在微信浏览器中打开和微信版本号
  8. 软件测试宝藏图_tSQLt –数据库单元测试中被遗忘的宝藏
  9. 如何使用sp_send_dbmail存储过程将SQL查询结果通过电子邮件发送到智能手机
  10. Android 常用 adb 命令总结【转】