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(, , size.width, size.height)];
result = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return result;
}

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

/**
将图片压缩到指定的质量 @param kb 1MB == 1024KB
@return 图片压缩后的质量
使用二分法提高效率,一般通过6次,可以达到指定的大小,也有例外,之后通过缩小尺寸来实现目标
*/
- (NSData *)FF_CompressQualityToSpecialKB:(CGFloat)kb {
CGFloat specialBytes = kb * ;
NSData *imageDate = UIImageJPEGRepresentation(self, 1.0);
if (imageDate.length < specialBytes) {
return imageDate;
}
CGFloat min = ;
CGFloat max = ;
CGFloat compress = ;
for (int i = ; i < ; i++) {
compress = (max + min) / ;
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 = ;
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(, , tempSize.width, tempSize.height)];
tempImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
imageDate = UIImageJPEGRepresentation(tempImage, compress);
} return imageDate;
}

4 通过颜色获取图片

+ (UIImage *)FF_AcquireImageFromColor:(UIColor *)color {
UIGraphicsBeginImageContext(CGSizeMake(, ));
CGContextRef ref = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(ref, color.CGColor);
CGContextFillRect(ref, CGRectMake(, , , ));
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:]];
} error:nil];
}

iOS-项目开发1-UIImage的更多相关文章

  1. iOS项目开发常用功能静态库

    YHDeveloperTools iOS项目开发常用功能静态库 查看源码 功能方法: 1.字符检查 [NSString checkStringWithType:Email andTargetStrin ...

  2. iOS项目开发实战——学会使用TableView列表控件(四)plist读取与Section显示

    文本将会实现把数据存储到plist文件里.然后在程序中进行读取.在TableView控件中依据不同的类别显示Section. 有关TableView 的其它实现,请參考<iOS项目开发实战--学 ...

  3. 聚合数据 iOS 项目开发实战:条码查询器

    记录下,聚合数据 iOS 项目开发实战:条码查询器:视频地址:http://www.jikexueyuan.com/course/324.html 条码查询API:https://www.juhe.c ...

  4. ios项目开发汇总

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

  5. iOS项目开发实战——使用CoreLocation获取当前位置信息

    随着基于位置服务LBS和移动互联网的兴起,你的位置是越来越重要的一个信息.位置服务已经是当前的热门应用如微信.陌陌等社交应用的杀手锏.而在iOS开发中,苹果已经给我们提供了一个位置接口.CoreLoc ...

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

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

  7. iOS项目开发实战——通过Http Get方式与server通信

    移动client往往须要同后台server进行通信,上传或者下载数据,最经常使用到的方式就是Http Get,如今我们来学习在iOS项目中使用Get方式同server进行通信. [一]server端实 ...

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

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

  9. iOS项目开发实战——plist数组解析

    plist数据是苹果公司创造的数据格式,基于XML,因为在iOS,Mac系统中操作plist很方便,所以我们经常会用到.在iOS项目中.系统会自己主动生成一个Info.plist文件,里面存放了iOS ...

  10. iOS项目开发日常之创建文件(协议、类、分类、扩展)

    iOS项目开发过程中,是以不断创建文件的形式进行着的. 创建得比较频繁的文件类型是: 这两个类型中创建的文件有:子类.分类.扩展.协议四种文件,如下:    这四类文件是频繁创建的,我们来看一下各自分 ...

随机推荐

  1. 59.加载Viewcontroller的几种方法(添加导航,解决xib里面空间不显示问题)

    // 一.根据StoryboardID(需要在Storyboard设置),通过ViewController所在的Storyboard来加载: UIStoryboard *storyboard = [U ...

  2. socketserver模块实现并发和连接合法性验证

    一.socketserver模块 1.sockeserver的源码流程 2.简单的使用 socketserver服务端 import socketserver class MyServer(socke ...

  3. shell 报错 /bin/bash^M: bad interpreter: No such file or directory

    shell 执行报错: ./test.sh: /bin/bash^M: bad interpreter: No such file or directory 原因:window和linux 的文件格式 ...

  4. Spring mvc项目,使用jetty插件和tomcat路径相差一个项目名

    pom.xml: jetty 插件配置: <plugin> <groupId>org.mortbay.jetty</groupId> <artifactId& ...

  5. 算法工程师B

    美团点评2017校招笔试真题-算法工程师B   1.以下关于经典的k-means聚类的说法哪个是错误的? A:k-means聚类算法是全局收敛的 B:k-means的聚类结果和初始聚类中心点的选取有关 ...

  6. 使用PostSharp在.NET平台上实现AOP(转)

    出处:https://www.cnblogs.com/leoo2sk/archive/2010/11/30/aop-postsharp.html 摘要 本文首先介绍AOP(面向方面编程)的相关概念及理 ...

  7. Codeforces Round #541 (Div. 2) G dp + 思维 + 单调栈 or 链表 (连锁反应)

    https://codeforces.com/contest/1131/problem/G 题意 给你一排m个的骨牌(m<=1e7),每块之间相距1,每块高h[i],推倒代价c[i],假如\(a ...

  8. vue +bootstrap 写的小例子

    最近vue挺火,最近也不是特别忙,就学习了下. vue和angular非常像都是MVVM.道理都是想通的,就是语法的差异 我觉得vue和angular区别: 1.vue更轻,更便捷,适用于移动开发 2 ...

  9. C语言程序设计50例(三)(经典收藏)

    [程序31]题目:请输入星期几的第一个字母来判断一下是星期几,如果第一个字母一样,则继续 判断第二个字母.1.程序分析:用情况语句比较好,如果第一个字母一样,则判断用情况语句或if语句判断第二个字母. ...

  10. s4-2 ALOHA 协议

    多路访问协议  随机访问协议(Random Access) 特点:站点争用信道,可能出现站点之间的冲突 典型的随机访问协议 • ALOHA协议 • CSMA协议 • CSMA/CD协议(以太网采 ...