图片压缩处理
来源:互联网 发布:java 设计通用api接口 编辑:程序博客网 时间:2024/05/16 11:23
#import <UIKit/UIKit.h>
@interface UIImageTransformation : UIImage{
}
+ (UIImage*)imageWithImageSimple:(UIImage*)image scaledToSize:(CGSize)newSize;
+ (UIImage*)imageByScalingAndCroppingForSize:(CGSize)targetSize image:(UIImage*)image;
#pragma mark - image scale utility
+ (UIImage *)imageByScalingToMaxSize:(UIImage *)sourceImage;
+ (UIImage *)imageByScalingAndCroppingForSourceImage:(UIImage *)sourceImage targetSize:(CGSize)targetSize;
@end
#import "UIImageTransformation.h"
#import "DeviceManage.h"
@implementation UIImageTransformation
-(id)init{
self = [super init];
return self;
}
+ (UIImage*)imageByScalingAndCroppingForSize:(CGSize)targetSize image:(UIImage*)image
{
// DLog(@"the image width is :%f", image.size.width);
// DLog(@"the image height is :%f", image.size.height);
//UIImage 压缩后的图片
UIImage *newImage = nil;
//原图片大小
CGSize imageSize = image.size;
//原图片宽度
CGFloat width = imageSize.width;
//原图片高度
CGFloat height = imageSize.height;
//欲压缩宽度
CGFloat targetWidth = targetSize.width;
//欲压缩高度
CGFloat targetHeight = targetSize.height;
//压缩比例系数(初始化为0)
CGFloat scaleFactor = 0.0;
//图片的宽度和高度小于欲压缩的宽度和高度,不需要压缩
if (width <= targetWidth && height <= targetHeight) {
return image;
}
//图片的宽度和高度都等于0,不需要压缩
if (width == 0 || height == 0) {
return image;
}
//宽度比例
CGFloat scaledWidth = targetWidth;
//高度比例
CGFloat scaledHeight = targetHeight;
//压缩点
CGPoint thumbnailPoint = CGPointMake(0.0,0.0);
/*
如果原图片大小跟欲压缩大小相同,则不执行压缩动作,否则执行压缩动作
执行:
*/
if ((CGSizeEqualToSize(imageSize, targetSize) == NO)&&(imageSize.width!=0)&&(imageSize.height!=0))
{
//宽度系数,格式 50/100=0.5
CGFloat widthFactor = targetWidth / width;
//高度系数 格式 50/100=0.5
CGFloat heightFactor = targetHeight / height;
if (widthFactor > heightFactor)
scaleFactor = widthFactor; // scale to fit height
else
scaleFactor = heightFactor; // scale to fit width
//压缩后的宽度=原宽度*宽度压缩系数
scaledWidth = width * scaleFactor;
//压缩后的宽度=原高度*高度压缩系数
scaledHeight = height * scaleFactor;
// center the image
if (widthFactor > heightFactor)
{
thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
}
else
if (widthFactor < heightFactor)
{
thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
}
}
//以下执行压缩动作
UIGraphicsBeginImageContext(targetSize); // this will crop
CGRect thumbnailRect = CGRectZero;
thumbnailRect.origin = thumbnailPoint;
thumbnailRect.size.width = scaledWidth;
thumbnailRect.size.height = scaledHeight;
[image drawInRect:thumbnailRect];
newImage = UIGraphicsGetImageFromCurrentImageContext();
DLog(@"new width is :%f", newImage.size.width);
DLog(@"new height is :%f", newImage.size.height);
if(newImage == nil)
//pop the context to get back to the default
UIGraphicsEndImageContext();
return newImage;
}
#pragma mark - image scale utility
+ (UIImage *)imageByScalingToMaxSize:(UIImage *)sourceImage {
// if (sourceImage.size.width/2 < ScreenWidth)
// return sourceImage;
CGFloat btWidth = 0.0f;
CGFloat btHeight = 0.0f;
CGFloat screenWidth = [[DeviceManage deviceManage] screenWidth];
if (sourceImage.size.height/2 < screenWidth) {
btHeight = screenWidth*2;
btWidth = sourceImage.size.width * (screenWidth*2/sourceImage.size.height);
// btWidth = sourceImage.size.width * (ScreenWidth*2 / sourceImage.size.width);
CGSize targetSize = CGSizeMake(btWidth, btHeight);
return [self imageByScalingAndCroppingForSourceImage:sourceImage targetSize:targetSize];
}
if (sourceImage.size.width > sourceImage.size.height) {
btHeight = screenWidth*2;
btWidth = sourceImage.size.width * (screenWidth*2 / sourceImage.size.height);
} else {
btWidth = screenWidth*2 ;
btHeight = sourceImage.size.height * (screenWidth*2 / sourceImage.size.width);
}
CGSize targetSize = CGSizeMake(btWidth, btHeight);
return [self imageByScalingAndCroppingForSourceImage:sourceImage targetSize:targetSize];
}
+ (UIImage *)imageByScalingAndCroppingForSourceImage:(UIImage *)sourceImage targetSize:(CGSize)targetSize {
UIImage *newImage = nil;
CGSize imageSize = sourceImage.size;
CGFloat width = imageSize.width;
CGFloat height = imageSize.height;
CGFloat targetWidth = targetSize.width;
CGFloat targetHeight = targetSize.height;
CGFloat scaleFactor = 0.0;
CGFloat scaledWidth = targetWidth;
CGFloat scaledHeight = targetHeight;
CGPoint thumbnailPoint = CGPointMake(0.0,0.0);
if (CGSizeEqualToSize(imageSize, targetSize) == NO)
{
CGFloat widthFactor = targetWidth / width;
CGFloat heightFactor = targetHeight / height;
if (widthFactor > heightFactor)
scaleFactor = widthFactor; // scale to fit height
else
scaleFactor = heightFactor; // scale to fit width
scaledWidth = width * scaleFactor;
scaledHeight = height * scaleFactor;
// center the image
if (widthFactor > heightFactor){
thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
}else if (widthFactor < heightFactor){
thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
}
}
UIGraphicsBeginImageContext(targetSize); // this will crop
CGRect thumbnailRect = CGRectZero;
thumbnailRect.origin = thumbnailPoint;
thumbnailRect.size.width = scaledWidth;
thumbnailRect.size.height = scaledHeight;
[sourceImage drawInRect:thumbnailRect];
newImage = UIGraphicsGetImageFromCurrentImageContext();
if(newImage == nil) NSLog(@"could not scale image");
//pop the context to get back to the default
UIGraphicsEndImageContext();
return newImage;
}
//压缩图片
+ (UIImage*)imageWithImageSimple:(UIImage*)image scaledToSize:(CGSize)newSize
{
// Create a graphics image context
UIGraphicsBeginImageContext(newSize);
// Tell the old image to draw in this new context, with the desired
// new size
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
// Get the new image from the context
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
// End the context
UIGraphicsEndImageContext();
// Return the new image.
return newImage;
}
@end
- 图片处理--压缩图片
- 图片压缩处理
- 图片上传压缩处理
- 图片压缩处理
- Java图片压缩处理
- 图片上传压缩处理
- Thumbnailator(图片压缩处理)
- java处理压缩图片
- C#图片压缩处理
- 图片上传压缩处理
- Java图片压缩处理
- java压缩图片处理
- iOS图片压缩处理
- 图片压缩处理
- iOS图片压缩处理
- Android图片压缩处理
- Java图片压缩处理
- iOS压缩图片处理
- 筛选100万以内的素数
- 代码阅读笔记——vl_nncrop
- Floodlight控制器创建一个模块的简单过程
- codeforces 303C Minimum Modular(下标的艺术+math)
- iOS开发之单元测试
- 图片压缩处理
- ddl dml
- csv 导出数据到csv或者xls文件
- nodejs 环境搭建
- 百万级PHP网站架构工具箱
- C语言开发总结(二十五)
- Linux ALSA声卡驱动之五:移动设备中的ALSA(ASoC)
- 谷歌AdMob广告接入(横幅广告)
- Cocos2d-js笔记