iOS-项目开发1-UIImage
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的更多相关文章
- iOS项目开发常用功能静态库
YHDeveloperTools iOS项目开发常用功能静态库 查看源码 功能方法: 1.字符检查 [NSString checkStringWithType:Email andTargetStrin ...
- iOS项目开发实战——学会使用TableView列表控件(四)plist读取与Section显示
文本将会实现把数据存储到plist文件里.然后在程序中进行读取.在TableView控件中依据不同的类别显示Section. 有关TableView 的其它实现,请參考<iOS项目开发实战--学 ...
- 聚合数据 iOS 项目开发实战:条码查询器
记录下,聚合数据 iOS 项目开发实战:条码查询器:视频地址:http://www.jikexueyuan.com/course/324.html 条码查询API:https://www.juhe.c ...
- ios项目开发汇总
UI界面 iOS和Android 界面设计尺寸规范 http://www.alibuybuy.com/posts/85486.html iPhone app界面设计尺寸规范 http://www. ...
- iOS项目开发实战——使用CoreLocation获取当前位置信息
随着基于位置服务LBS和移动互联网的兴起,你的位置是越来越重要的一个信息.位置服务已经是当前的热门应用如微信.陌陌等社交应用的杀手锏.而在iOS开发中,苹果已经给我们提供了一个位置接口.CoreLoc ...
- iOS项目开发优秀文章汇总
UI界面 iOS和Android 界面设计尺寸规范 http://www.alibuybuy.com/posts/85486.html iPhone app界面设计尺寸规范 http://www. ...
- iOS项目开发实战——通过Http Get方式与server通信
移动client往往须要同后台server进行通信,上传或者下载数据,最经常使用到的方式就是Http Get,如今我们来学习在iOS项目中使用Get方式同server进行通信. [一]server端实 ...
- iOS项目开发实战——iOS网络编程获取网页Html源码
现在我们身处互联网的时代.不论什么一个软件或是App,都会或多或少与网络打交道,并不断发生数据交互.一个没有涉及网络编程的应用会显得比較low,这里我们将会開始使用Swift开发iOS应用,而且主要来 ...
- iOS项目开发实战——plist数组解析
plist数据是苹果公司创造的数据格式,基于XML,因为在iOS,Mac系统中操作plist很方便,所以我们经常会用到.在iOS项目中.系统会自己主动生成一个Info.plist文件,里面存放了iOS ...
- iOS项目开发日常之创建文件(协议、类、分类、扩展)
iOS项目开发过程中,是以不断创建文件的形式进行着的. 创建得比较频繁的文件类型是: 这两个类型中创建的文件有:子类.分类.扩展.协议四种文件,如下: 这四类文件是频繁创建的,我们来看一下各自分 ...
随机推荐
- 【Web】Nginx Rewrite规则
Rewrite介绍 Rewrite主要的功能就是实现URL的重写,Nginx的Rewrite规则采用Pcre,perl兼容正则表达式的语法规则匹配,如果需要Nginx的Rewrite功能,在编译Ngi ...
- 20155312 2016-2017-2 《Java程序设计》第八周学习总结
20155312 2016-2017-2 <Java程序设计>第八周学习总结 课堂内容总结 学习模式 游乐园模式-荒野求生模式 学习方法 以代码为中心->遇到不会的类和方法(参数等) ...
- 如何使用GCC生成动态库和静态库
根据链接时期的不同,库又有静态库和动态库之分.静态库是在链接阶段被链接的,所以生成的可执行文件就不受库的影响,即使库被删除,程序依然可以成功运行.而动态库是在程序执行的时候被链接的.程序执行完,库仍需 ...
- BP神经网络在python下的自主搭建梳理
本实验使用mnist数据集完成手写数字识别的测试.识别正确率认为是95% 完整代码如下: #!/usr/bin/env python # coding: utf-8 # In[1]: import n ...
- python 中 __name__ 的使用
1. 如果模块是被导入,__name__的值为模块名字2. 如果模块是被直接执行,__name__的值为’__main__’ Py1.py #!/usr/bin/env python def te ...
- from __future__ import
读代码的过程中看到的,好奇搜索了一下,其实当在我们调试别人Python代码的过程中经常会遇到一些问题,比如版本不同,代码也会有所改变,比如print函数 Python 2.7版本为 print “ ” ...
- 2018.11.02 洛谷P2831 愤怒的小鸟(状压dp)
传送门 状压一眼题. 直接f[i]f[i]f[i]表示未选择状态为iii时的最小次数. 然后考虑现在怎么转移. 显然可以直接枚举消掉某一个点或者某两个点,复杂度O(n22n)O(n^22^n)O(n2 ...
- 2018.11.01 NOIP训练 递增数列(迭代加深)
传送门 直接迭代加深搜索. 发现每次最多增加一倍,最少增加一,于是果断上下界剪枝. 代码
- vue-cli项目中使用rem
1.安装px2rem插件 npm install px2rem-loader lib-flexible --save 2.在main.js中引入lib-flexible import 'lib-fle ...
- 三个UID
1.三个UID 这三个UID分别是实际用户ID(real uid).有效用户ID(effective uid).保存的设置用户ID(saved set-user-ID)(SUID) 实际用户ID(RU ...