IOS开发基础之——图像压缩成指定大小(高度/宽度)
来源:互联网 发布:it运维软件 编辑:程序博客网 时间:2024/05/17 21:42
.h文件:
#import <Foundation/Foundation.h>
@interface UIImage (UIImageExt)
- (UIImage*)imageByScalingAndCroppingForSize:(CGSize)targetSize;
@end
.m文件:
#import "UIImageExt.h"
@implementation UIImage (UIImageExt)
- (UIImage*)imageByScalingAndCroppingForSize:(CGSize)targetSize
{
UIImage *sourceImage = self;
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;
}
@end
- IOS开发基础之——图像压缩成指定大小(高度/宽度)
- IOS开发基础之——图像压缩成指定大小(高度/宽度)
- IOS开发基础之——图像压缩成指定大小(高度/宽度)
- IOS开发基础之——图像压缩成指定大小(高度/宽度)
- 图像压缩成指定大小(高度/宽度)
- 图像压缩成指定大小(高度/宽度)
- iOS开发技巧之:图片压缩成指定的大小
- iOS开发-计算label文本大小尺寸高度宽度
- android开发游记:图像文件压缩方法总结(将图像压缩到指定大小)
- iOS图片压缩成指定大小
- iOS图片压缩成指定大小
- Atitit nodejs js 获取图像分辨率 尺寸 大小 宽度 高度
- iOS中图片压缩成指定的大小
- Javascript获取窗口大小(宽度,高度)
- 【iOS 开发】Xcode6中计算文字的宽度或高度 ——伊禾媛
- IOS中将照片压缩至指定大小
- IOS 压缩图片到指定大小kb
- 图片压缩成指定大小
- CHRTRAN() 函数
- OpenEIM 第一次接触和使用Sqlite
- Android中处理崩溃异常
- 显示scrollview时对滚动条进行任意位置的定位 .
- POJ 1564 深搜
- IOS开发基础之——图像压缩成指定大小(高度/宽度)
- Android] Android开发优化之——使用软引用和弱引用
- 关于express与ejs的那点儿事
- 管式超滤系统:中空纤维超滤系统概述
- PPI Services
- 驾校考试理论考的一些技巧你知道吗?
- U-boot源码下载地址
- STC89C52RC内部EEPROM的读写
- c++ 数字转汉字