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. jvm层面锁优化+一般锁的优化策略

    偏向锁: 首先了解对象头MARK指针(对象头标记,32位): 存储GC标记,对象年龄,对象Hash,锁信息(锁记录的指针,偏向锁线程的ID) 大部分情况是没有竞争的,所以可以通过偏向来提高性能 所谓的 ...

  2. 【Apache】Apache服务的安装(一)

    Apache简介 Apache HTTP Server(简称Apache)是Apache软件基金会的一个开放源码的网页服务器,可以在大多数计算机操作系统中运行,由于其多平台和安全性被广泛使用,是最流行 ...

  3. 兼容ie透明书写

    filter:alpha(opacity=0); opacity:0;filter:alpha(opacity=70); opacity:0.7;

  4. 在python中while

    一.While循环 1.while循环格式 while 条件 : while循环体 当条件为Ture时,执行循环体,直到条件是假,停止循环. count = 1 # count 计数 一般用于计数 w ...

  5. 2018.10.31 NOIP训练 锻造(方程式期望入门题)(期望dp)

    传送门 根据题目列出方程: fi=pi∗(fi−1+fi−2)+(1−pi)∗(fi+1+fi)f_i=p_i*(f_{i-1}+f_{i-2})+(1-p_i)*(f_{i+1}+f_i)fi​=p ...

  6. openstack之虚拟机管理命令

    在控制节点上建hzb-openrc.sh export OS_PROJECT_DOMAIN_ID=default export OS_USER_DOMAIN_ID=default export OS_ ...

  7. c++中typedef、define、const、inline之间的区别

    1.typedef和#define的区别 typedef int* pInt; , b = ; const pInt p1 = &a; //p1是常量指针 pInt const p2 = &a ...

  8. Maven进行install的时候报错,COMPILATION ERROR : Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.13:test (default-test) on project cmu: There are test failures.

    maven进行install的时候,test类里面报错: COMPILATION ERROR : [INFO] -------------------------------------------- ...

  9. Mysql之数据库操作

    数据库操作: 链接数据库: mysql -uroot -p masql -uroot -pmysql 退出数据库: exit/quit/ctrl + d   sql语句最后需要分号结尾: 查看时间: ...

  10. BAT的真的适合创业团队吗?

    平时在公司扮演一个逗比得角色和亲爱的们友好相处的我根本不愿意去思考这么深入的课题.本来在上一家公司就涉及的太深,心爱的一条小产品线被咔掉后心疼不已.只想深入研究技术不问世事了.怎奈何突然有一天说要招一 ...