IOS开发基础之——图像压缩成指定大小(高度/宽度)
来源:互联网 发布:融信网网络贷款 编辑:程序博客网 时间:2024/05/22 07:56
.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
- 图片压缩成指定大小
- ubuntu设置时区,网上同步时间
- 一些iOS开源类库及工具
- cxf+spring完成webservice发布
- Linux 编程经典书籍
- Hadoop源代码分析 共41篇(收藏)
- IOS开发基础之——图像压缩成指定大小(高度/宽度)
- 从windows batch shell启动selenium自动测试并获取report
- Solaris10修改主机名
- 如何通过Makefile编译iphone的openSSL
- C语言 通过指针从函数返回一个数组
- android Preference之android:dependency
- 转别人一个样式好看的导航
- hbase开启lzo压缩
- SD controller CRC error 分析