IOS :图像压缩
2012-11-30 17:11
120 查看
.h文件:
#import <Foundation/Foundation.h>
@interface UtilMethods : NSObjects
- (UIImage*)imageByScalingAndCroppingForSize:(CGSize)targetSize withImage:(UIImage *)sourceImage;
@end
.m文件:
#import "UtilMethods.h"
@implementation
UtilMethods
- (UIImage*)imageByScalingAndCroppingForSize:(CGSize)targetSize withImage:(UIImage *)sourceImage
{
UIImage *newImage = nil;
CGFloat width =
sourceImage.size.width;
CGFloat height =
sourceImage.size.height;
CGFloat targetWidth = targetSize.width;
CGFloat targetHeight = targetSize.height;
CGFloat scaleFactor = 0.0;
CGFloat scaledX = 0.0;
CGFloat scaledY = 0.0;
CGFloat scaledWidth = targetWidth;
CGFloat scaledHeight = targetHeight;
CGPoint
thumbnailRect = CGSizeMake(0.0,0.0,0.0,0.0);
if (CGSizeEqualToSize(imageSize, targetSize) == NO)
{
CGFloat widthFactor = targetWidth / width;
CGFloat heightFactor = targetHeight / height;
if (widthFactor > heightFactor)
scaleFactor =
heightFactor; // scale to fit
width
else
scaleFactor =
widthFactor; // scale to fit
height
scaledWidth= width * scaleFactor;
scaledHeight = height * scaleFactor;
// center the image
if (widthFactor > heightFactor)
{
scaledY
= (targetHeight - scaledHeight)/2;
}
else if (widthFactor < heightFactor)
{
scaledX = (targetWidth - scaledWidth)/2;
}
}
UIGraphicsBeginImageContext(targetSize);
thumbnailRect = CGSizeMake(scaledX,scaledY,scaledWidth,scaledHeight);
[sourceImage drawInRect:thumbnailRect];
newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
@end
#import <Foundation/Foundation.h>
@interface UtilMethods : NSObjects
- (UIImage*)imageByScalingAndCroppingForSize:(CGSize)targetSize withImage:(UIImage *)sourceImage;
@end
.m文件:
#import "UtilMethods.h"
@implementation
UtilMethods
- (UIImage*)imageByScalingAndCroppingForSize:(CGSize)targetSize withImage:(UIImage *)sourceImage
{
UIImage *newImage = nil;
CGFloat width =
sourceImage.size.width;
CGFloat height =
sourceImage.size.height;
CGFloat targetWidth = targetSize.width;
CGFloat targetHeight = targetSize.height;
CGFloat scaleFactor = 0.0;
CGFloat scaledX = 0.0;
CGFloat scaledY = 0.0;
CGFloat scaledWidth = targetWidth;
CGFloat scaledHeight = targetHeight;
CGPoint
thumbnailRect = CGSizeMake(0.0,0.0,0.0,0.0);
if (CGSizeEqualToSize(imageSize, targetSize) == NO)
{
CGFloat widthFactor = targetWidth / width;
CGFloat heightFactor = targetHeight / height;
if (widthFactor > heightFactor)
scaleFactor =
heightFactor; // scale to fit
width
else
scaleFactor =
widthFactor; // scale to fit
height
scaledWidth= width * scaleFactor;
scaledHeight = height * scaleFactor;
// center the image
if (widthFactor > heightFactor)
{
scaledY
= (targetHeight - scaledHeight)/2;
}
else if (widthFactor < heightFactor)
{
scaledX = (targetWidth - scaledWidth)/2;
}
}
UIGraphicsBeginImageContext(targetSize);
thumbnailRect = CGSizeMake(scaledX,scaledY,scaledWidth,scaledHeight);
[sourceImage drawInRect:thumbnailRect];
newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
@end
相关文章推荐
- iOS开发图像压缩处理问题
- iOS对图像进行尺寸压缩
- ios中图像进行压缩方法汇总
- IOS图像2之jpg、png图片保存、互转、设置有损压缩因子
- IOS开发基础之——图像压缩成指定大小(高度/宽度)
- iOS常用小功能(获得屏幕图像、压缩图片、加边框、调整label的size)
- IOS开发基础之——图像压缩成指定大小(高度/宽度)
- IOS开发基础之——图像压缩成指定大小(高度/宽度)
- ios中图像进行压缩方法汇总
- iOS常用小功能(获得屏幕图像、压缩图片、加边框、调整label的size)
- iOS常用小功能(获得屏幕图像、压缩图片、加边框、调整label的size)
- iOS开发-进阶:图像进行压缩方法汇总
- IOS开发基础之——图像压缩成指定大小(高度/宽度)
- iOS基于AVFoundation + GPUImage的实时图像处理
- IOS视频压缩
- iOS视频拍摄与压缩
- 利用jpeglib压缩各种图像格式为jpg格式,linux下bmp转化为jpeg程序源代码
- ios中摄像头/相册获取图片,压缩图片,上传服务器方法总结
- iOS使用CoreImage处理图像40中可用的滤镜名称
- 在iOS使用ZipArchive压缩和解压缩文件