|
/*!
|
* @header BAKit.h
|
* BABaseProject
|
*
|
* @brief BAKit
|
*
|
* @author 博爱
|
* @copyright Copyright © 2016年 博爱. All rights reserved.
|
* @version V1.0
|
*/
|
|
// _ooOoo_
|
// o8888888o
|
// 88" . "88
|
// (| -_- |)
|
// O\ = /O
|
// ____/`---'\____
|
// . ' \\| |// `.
|
// / \\||| : |||// \
|
// / _||||| -:- |||||- \
|
// | | \\\ - /// | |
|
// | \_| ''\---/'' | |
|
// \ .-\__ `-` ___/-. /
|
// ___`. .' /--.--\ `. . __
|
// ."" '< `.___\_<|>_/___.' >'"".
|
// | | : `- \`.;`\ _ /`;.`/ - ` : | |
|
// \ \ `-. \_ __\ /__ _/ .-` / /
|
// ======`-.____`-.___\_____/___.-`____.-'======
|
// `=---='
|
//
|
// .............................................
|
// 佛祖镇楼 BUG辟易
|
// 佛曰:
|
// 写字楼里写字间,写字间里程序员;
|
// 程序人员写程序,又拿程序换酒钱。
|
// 酒醒只在网上坐,酒醉还来网下眠;
|
// 酒醉酒醒日复日,网上网下年复年。
|
// 但愿老死电脑间,不愿鞠躬老板前;
|
// 奔驰宝马贵者趣,公交自行程序员。
|
// 别人笑我忒疯癫,我笑自己命太贱;
|
// 不见满街漂亮妹,哪个归得程序员?
|
|
/*
|
|
*********************************************************************************
|
*
|
* 在使用BAKit的过程中如果出现bug请及时以以下任意一种方式联系我,我会及时修复bug
|
*
|
* QQ : 博爱1616【137361770】
|
* 微博 : 博爱1616
|
* Email : 137361770@qq.com
|
* GitHub : https://github.com/boai
|
* 博客园 : http://www.cnblogs.com/boai/
|
* 博客 : http://boai.github.io
|
|
*********************************************************************************
|
|
*/
|
|
#import "UIImage+CompressImage.h"
|
|
@implementation UIImage (CompressImage)
|
|
+(JPEGImage *)needCompressImage:(UIImage *)image size:(CGSize )size scale:(CGFloat )scale
|
{
|
JPEGImage *newImage = nil;
|
//创建画板
|
UIGraphicsBeginImageContext(size);
|
|
//写入图片
|
[image drawInRect:CGRectMake(0, 0, size.width, size.height)];
|
|
//得到新的图片
|
newImage = UIGraphicsGetImageFromCurrentImageContext();
|
|
//释放画板
|
UIGraphicsEndImageContext();
|
|
//比例压缩
|
newImage = [UIImage imageWithData:UIImageJPEGRepresentation(newImage, scale)];
|
// newImage = [UIImage imageWithData:UIImageJPEGRepresentation(newImage, 1.0) scale:scale];
|
|
return newImage;
|
}
|
|
+(JPEGImage *)needCompressImageData:(NSData *)imageData size:(CGSize )size scale:(CGFloat )scale
|
{
|
PNGImage *image = [UIImage imageWithData:imageData];
|
return [UIImage needCompressImage:image size:size scale:scale];
|
}
|
|
+ (JPEGImage *)needCenterImage:(UIImage *)image size:(CGSize )size scale:(CGFloat )scale
|
{
|
/* 想切中间部分,待解决 */
|
#warning area of center image
|
JPEGImage *newImage = nil;
|
//创建画板
|
UIGraphicsBeginImageContext(size);
|
|
//写入图片,在中间
|
[image drawInRect:CGRectMake(0, 0, size.width, size.height)];
|
|
//得到新的图片
|
newImage = UIGraphicsGetImageFromCurrentImageContext();
|
|
//释放画板
|
UIGraphicsEndImageContext();
|
|
//比例压缩
|
newImage = [UIImage imageWithData:UIImageJPEGRepresentation(newImage, scale)];
|
|
return newImage;
|
}
|
|
+(JPEGImage *)jpegImageWithPNGImage:(PNGImage *)pngImage
|
{
|
return [UIImage needCompressImage:pngImage size:pngImage.size scale:1.0];
|
}
|
|
+(JPEGImage *)jpegImageWithPNGData:(PNGData *)pngData
|
{
|
PNGImage *pngImage = [UIImage imageWithData:pngData];
|
return [UIImage needCompressImage:pngImage size:pngImage.size scale:1.0];
|
}
|
|
+(JPEGData *)jpegDataWithPNGData:(PNGData *)pngData
|
{
|
return UIImageJPEGRepresentation([UIImage jpegImageWithPNGData:pngData], 1.0);
|
}
|
|
+(JPEGData *)jpegDataWithPNGImage:(PNGImage *)pngImage
|
{
|
return UIImageJPEGRepresentation([UIImage jpegImageWithPNGImage:pngImage], 1.0);
|
}
|
@end
|