IOS图片压缩的类
来源:互联网 发布:伊朗女孩 知乎 编辑:程序博客网 时间:2024/06/05 15:10
//
// UIImage+UIImageExt.m
// MyHomeBuy
//
// Created by first on 13-9-13.
// Copyright (c) 2013年 __MyCompanyName__. All rights reserved.
//
#import "UIImage+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
//
// UIImage+UIImageExt.h
// MyHomeBuy
//
// Created by first on 13-9-13.
// Copyright (c) 2013年 __MyCompanyName__. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface UIImage (UIImageExt)
- (UIImage*)imageByScalingAndCroppingForSize:(CGSize)targetSize;
@end
- IOS图片压缩的类
- iOS 图片的压缩
- iOS 图片压缩的方法
- ios图片的压缩处理
- 压缩图片的类
- iOS 史上最全的图片压缩方法集合
- iOS 中图片的压缩以及保存
- ios 图片压缩的两种方式
- iOS 常用的图片压缩函数
- iOS开发上传图片的压缩
- iOS中图片压缩的常见方法
- iOS中常见的图片压缩方法
- iOS之图片压缩的方法
- iOS中图片压缩的常见方法
- iOS 压缩图片的方法
- iOS中图片压缩的常见方法
- IOS 图片压缩
- ios--图片压缩/缩放
- tomcat打开jsp文件中文乱码问题之解决
- 【安博培训技术】Java5 IO流 20130920
- GC日志分析
- Codeforces Round #201 (Div. 2) A. Difference Row
- 【安博培训技术】Java6 JDBC编程 20130921
- IOS图片压缩的类
- Codeforces Round #201 (Div. 2) B. Fixed Points
- 常用排序算法总结(二) ---- 插入排序,快速排序
- 工程师规范
- Codeforces Round #201 (Div. 2) C. Alice and Bob
- array and pointer
- 创业公司的切入点
- CSS复合选择器
- 如何写程序!!!