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项目开发过程中,是以不断创建文件的形式进行着的. 创建得比较频繁的文件类型是: 这两个类型中创建的文件有:子类.分类.扩展.协议四种文件,如下: 这四类文件是频繁创建的,我们来看一下各自分 ...
随机推荐
- canvas 实现飞碟射击游戏
var canvas = document.getElementById('canvas'); var cxt = canvas.getContext('2d'); // 射击角度 var ang = ...
- php获取响应状态码
$ch = curl_init('http://www.jb51.net'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_exec($ch); ...
- idea运行项目时报Error:java无效的源发行版:1.8
如果你安装的是JDK1.7,而在file->project structure中设置的是language level是8的话,就会出现这个错误提示:无效的源发行版:8. 解决办法:将语言级别改为 ...
- 深度学习中 epoch,[batch size], iterations概念解释
one epoch:所有的训练样本完成一次Forword运算以及一次BP运算 batch size:一次Forword运算以及BP运算中所需要的训练样本数目,其实深度学习每一次参数的更新所需要损失函数 ...
- 2019.02.09 codeforces gym 100548F. Color(容斥原理)
传送门 题意简述:对n个排成一排的物品涂色,有m种颜色可选. 要求相邻的物品颜色不相同,且总共恰好有K种颜色,问所有可行的方案数.(n,m≤1e9,k≤1e6n,m\le1e9,k\le1e6n,m≤ ...
- sql计算经纬度得出最近距离的公式
sql计算经纬度得出最近距离的公式 //根据经纬度计算两点距离 mappoint //数据库已有字段,商家经纬度 实例:113.272148,23.147299 $lon = "" ...
- 微信小程序之跨界面传参
微信小程序在两个之间传参类似js传递url拼接参数,举个例子来说吧 input自己设置参数 //index.wxml <form bindsubmit="formSubmit" ...
- verilog中的多维数组
reg arrayb [7:0] [0:255] ;//二维数组.
- Codeforces Round#415 Div.2
A. Straight «A» 题面 Noora is a student of one famous high school. It's her final year in school - she ...
- Kafka C++客户端库librdkafka笔记
目录 目录 1 1. 前言 2 2. 缩略语 2 3. 配置和主题 3 3.1. 配置和主题结构 3 3.1.1. Conf 3 3.1.2. ConfImpl 3 3.1.3. Topic 3 3. ...