UIImageExtension.swift
//
// UIImageExtension.swift
// GongFuBaoSwift
//
// Created by GongHui_YJ on 16/8/12.
// Copyright © 2016年 杨建. All rights reserved.
//
// _ooOoo_
// o8888888o
// 88" . "88
// (| -_- |)
// O\ = /O
// ____/`---'\____
// . ' \\| |// `.
// / \\||| : |||// \
// / _||||| -:- |||||- \
// | | \\\ - /// | |
// | \_| ''\---/'' | |
// \ .-\__ `-` ___/-. /
// ___`. .' /--.--\ `. . __
// ."" '< `.___\_<|>_/___.' >'"".
// | | : `- \`.;`\ _ /`;.`/ - ` : | |
// \ \ `-. \_ __\ /__ _/ .-` / /
// ======`-.____`-.___\_____/___.-`____.-'======
// `=---='
//
// .............................................
// 佛祖保佑 永无BUG
// 佛曰:
// 写字楼里写字间,写字间里程序员;
// 程序人员写程序,又拿程序换酒钱。
// 酒醒只在网上坐,酒醉还来网下眠;
// 酒醉酒醒日复日,网上网下年复年。
// 但愿老死电脑间,不愿鞠躬老板前;
// 奔驰宝马贵者趣,公交自行程序员。
// 别人笑我忒疯癫,我笑自己命太贱;
// 不见满街漂亮妹,哪个归得程序员?
import Foundation
extension UIImage {
///对指定图片进行拉伸
func resizableImage(name: String) -> UIImage {
var normal = UIImage(named: name)!
let imageWidth = normal.size.width * 0.5
let imageHeight = normal.size.height * 0.5
normal = resizableImageWithCapInsets(UIEdgeInsetsMake(imageHeight, imageWidth, imageHeight, imageWidth))
return normal
}
/**
* 压缩上传图片到指定字节
*
* image 压缩的图片
* maxLength 压缩后最大字节大小
*
* return 压缩后图片的二进制
*/
class func compressImage(image: UIImage, maxLength: Int) -> NSData? {
let newSize = self.scaleImage(image, imageLength: 300)
let newImage = self.resizeImage(image, newSize: newSize)
var compress:CGFloat = 0.9
var data = UIImageJPEGRepresentation(newImage, compress)
while data?.length > maxLength && compress > 0.01 {
compress -= 0.02
data = UIImageJPEGRepresentation(newImage, compress)
}
return data
}
/**
* 通过指定图片最长边,获得等比例的图片size
*
* image 原始图片
* imageLength 图片允许的最长宽度(高度)
*
* return 获得等比例的size
*/
class func scaleImage(image: UIImage, imageLength: CGFloat) -> CGSize {
var newWidth:CGFloat = 0.0
var newHeight:CGFloat = 0.0
let width = image.size.width
let height = image.size.height
if (width > imageLength || height > imageLength){
if (width > height) {
newWidth = imageLength;
newHeight = newWidth * height / width;
}else if(height > width){
newHeight = imageLength;
newWidth = newHeight * width / height;
}else{
newWidth = imageLength;
newHeight = imageLength;
}
}
return CGSize(width: newWidth, height: newHeight)
}
/**
* 获得指定size的图片
*
* image 原始图片
* newSize 指定的size
*
* return 调整后的图片
*/
class func resizeImage(image: UIImage, newSize: CGSize) -> UIImage {
UIGraphicsBeginImageContext(newSize)
image.drawInRect(CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height))
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage
}
}
OC版
/**
* 图片压缩
*/
- (NSData *)resetSizeOfImageData:(UIImage *)source_image maxSize:(NSInteger)maxSize
{
//先调整分辨率
CGSize newSize = CGSizeMake(source_image.size.width, source_image.size.height);
CGFloat tempHeight = newSize.height / 1024;
CGFloat tempWidth = newSize.width / 1024;
if (tempWidth > 1.0 && tempWidth > tempHeight) {
newSize = CGSizeMake(source_image.size.width / tempWidth, source_image.size.height / tempWidth);
}
else if (tempHeight > 1.0 && tempWidth < tempHeight){
newSize = CGSizeMake(source_image.size.width / tempHeight, source_image.size.height / tempHeight);
}
UIGraphicsBeginImageContext(newSize);
[source_image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//调整大小
NSData *imageData = UIImageJPEGRepresentation(newImage,1.0);
NSUInteger sizeOrigin = [imageData length];
NSUInteger sizeOriginKB = sizeOrigin / 1024;
if (sizeOriginKB > maxSize) {
NSData *finallImageData = UIImageJPEGRepresentation(newImage,0.50);
return finallImageData;
}
return imageData;
}