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. setInterval与setTimeout 的区别

    setInterval在执行完一次代码之后,经过了那个固定的时间间隔,它还会自动重复执行代码,而setTimeout只执行一次那段代码     用法: setInterval("alert( ...

  2. ueditor 功能定制

    方法一:用js传参 var editor = new UE.ui.Editor({initialFrameHeight:200,initialFrameWidth:640,toolbars:[[&qu ...

  3. Time.fixedDeltaTime和Time.DeltaTime

    在Update中使用 Time.deltaTime,获取到的是这一帧的时间,如果游戏卡,帧率低,那这个值就大.如果游戏流畅,帧率高,这个值就小,Time.deltaTime = 1.0f / 帧率 在 ...

  4. Firefox table 不居中解决办法 解决火狐层或 table 不居中

    Firefox table 不居中解决办法: table 使用 align="center" ,IE正常,Firefox 却是居左了,网上有各种解决的办法,比如在table外面再套 ...

  5. 第11章:MongoDB-CRUD操作--文档--查询

    ①语法 db.collection.find(query, projection) ②参数 query :可选,使用查询操作符指定查询条件 projection :可选,使用投影操作符指定返回的键.查 ...

  6. 查阅JDK,collection与collections区别大

    看起来collection,和collections相像,但其中的差别之大你造吗? Collection是Collection层次结构中的根接口.Collection表示一组对象,这也对象也称为col ...

  7. Effective C++ 随笔(5)

    条款27:尽量稍作转型动作 const_cast:常量性移除 dynamic_cast:安全向下转型 reinterpret_cast: static_cast: 如在子类当中享调用父类当中的某个方法 ...

  8. 最顶尖的12个IT技能

    这差不多是十年前得了,看看今天这些东西哪些死掉了,哪些成长茁壮了,又能有哪些启示. KevinScott是谷歌公司的高级技术经理,也是美国计算机协会专业与教育委 员会的创始成员,他说:“我在硅谷看到的 ...

  9. 网络通信协议简介(TCP与UDP)

    通过计算机网络可以使多台计算机实现连接,位于同一个网络中的计算机在进行连接和通信时需要遵守一定的规则,这就好比在道路中行驶的汽车一定要遵守交通规则一样.在计算机网络中,这些连接和通信的规则被称为网络通 ...

  10. SRM480

    250pt: 题意:给定n个网站,以及n个网站的关键词,还有一个危险词库.如果一个网站的关键词中>=th的危险词,那么这个网站便是危险的.同时,他的所有关键词加入危险词库.问,有多少个危险网站. ...