文章转载于知乎 砥砺
//将图片剪裁成圆形
class func imagewithImage(image:UIImage) -> UIImage{
let width = image.size.width
let height = image.size.height
let redius = ((width <= height) ? width : height)/2;
let rect = CGRect(x: width/2-redius,y: height/2-redius,width: redius*2,height: redius*2);
let sourceImageRef = image.cgImage
let newImageRef = sourceImageRef?.cropping(to: rect)
let newImage = UIImage(cgImage: newImageRef!)
UIGraphicsBeginImageContextWithOptions(CGSize(width:newImage.size.width,height: newImage.size.height), false, 0);
let path = UIBezierPath(arcCenter: CGPoint(x: newImage.size.width / 2, y: newImage.size.height / 2), radius: redius, startAngle: 0, endAngle:CGFloat(Double.pi * 2.0), clockwise: false)
path.addClip()
newImage.draw(at: CGPoint.zero)
let imageCut = UIGraphicsGetImageFromCurrentImageContext()
return imageCut!
}
//重设图片大小
func reSizeImage(reSize:CGSize)->UIImage {
//UIGraphicsBeginImageContext(reSize)
UIGraphicsBeginImageContextWithOptions(reSize,false,UIScreen.main.scale)
self.draw(in: CGRect(x: 0,y: 0,width: reSize.width,height: reSize.height))
let reSizeImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
return reSizeImage
}
//OC版
(UIImage *) scaleImage:(UIImage *)image toScale:(float)scaleSize {
UIGraphicsBeginImageContext(CGSizeMake(image.size.width * scaleSize, image.size.height * scaleSize);
[image drawInRect:CGRectMake(0, 0, image.size.width * scaleSize, image.size.height * scaleSize)];
UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return scaledImage;
}
//等比率缩放
func scaleImage(scaleSize:CGFloat)->UIImage {
let reSize = CGSize(width:self.size.width * scaleSize,height: self.size.height * scaleSize)
return reSizeImage(reSize: reSize)
}
/**
* 压缩上传图片到指定字节
* image 压缩的图片
* maxLength 压缩后最大字节大小
* return 压缩后图片的二进制
*/
func compressImage(image: UIImage, maxLength: Int) -> NSData? {
let newSize = self.scaleImage(image: image, imageLength: 300)
let newImage = self.resizeImage(image: image, newSize: newSize)
var compress:CGFloat = 0.9
var data = UIImageJPEGRepresentation(newImage, compress)
while (data?.count)! > maxLength && compress > 0.01 {
ompress -= 0.02
data = UIImageJPEGRepresentation(newImage, compress)
}
return data! as NSData
}
/**
* 通过指定图片最长边,获得等比例的图片size
* image 原始图片
* imageLength 图片允许的最长宽度(高度)
* return 获得等比例的size
*/
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 调整后的图片
*/
func resizeImage(image: UIImage, newSize: CGSize) -> UIImage {
UIGraphicsBeginImageContext(newSize)
image.draw(in: CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height))
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage!
}