Swift开发教程--有关图片处理的一些有用函数
Posted wanglixin1999
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Swift开发教程--有关图片处理的一些有用函数相关的知识,希望对你有一定的参考价值。
/**
* 切圆形图片
*
* @param image:UIImage
* @param inset:CGFloat
*
* @return UIImage
*/
class func circleImage(image:UIImage,inset:CGFloat) -> UIImage
UIGraphicsBeginImageContext(image.size);
var context:CGContextRef = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 6);
CGContextSetStrokeColorWithColor(context, UIColor.whiteColor().CGColor);
var rect:CGRect = CGRectMake(inset, inset, (image.size.width-inset*2), (image.size.height-inset*2));
CGContextAddEllipseInRect(context, rect);
CGContextClip(context);
image.drawInRect(rect);
CGContextAddEllipseInRect(context, rect);
CGContextStrokePath(context);
var newImg:UIImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImg;
/**
* 重设图片大小
*
* @param image:UIImage
* @param reSize:CGSize
*
* @return UIImage
*/
class func reSizeImage(image:UIImage,reSize:CGSize)->UIImage
UIGraphicsBeginImageContext(CGSizeMake(reSize.width, reSize.height));
image.drawInRect(CGRectMake(0, 0, reSize.width, reSize.height));
var reSizeImage:UIImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return reSizeImage;
/**
* 等比率缩放
*
* @param image:UIImage
* @param scaleSize:CGFloat
*
* @return UIImage
*/
class func scaleImage(image:UIImage,scaleSize:CGFloat)->UIImage
UIGraphicsBeginImageContext(CGSizeMake(image.size.width*scaleSize, image.size.height*scaleSize));
image.drawInRect(CGRectMake(0, 0, image.size.width*scaleSize, image.size.height*scaleSize));
var scaledImage:UIImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return scaledImage;
/**
* 3.处理某个特定View
只要是继承UIView的object 都可以处理
必须先import QuzrtzCore.framework
*
* @param theView UIView
*
* @return UIImage
*/
class func captureView(theView:UIView)->UIImage
var rect:CGRect = theView.frame;
UIGraphicsBeginImageContext(rect.size);
var context:CGContextRef = UIGraphicsGetCurrentContext();
theView.layer.renderInContext(context);
var img:UIImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
/**
* 把图片以filename名称存到app home下的Documents目录里
*
* @param image:UIImage
* @param filename:NSString
*
* @return
*/
class func saveImageFile(image:UIImage,filename:NSString)
var path:NSString = NSHomeDirectory().stringByAppendingPathComponent("Documents").stringByAppendingPathComponent(filename as String);
UIImagePNGRepresentation(image).writeToFile(path as String, atomically: true);
以上是关于Swift开发教程--有关图片处理的一些有用函数的主要内容,如果未能解决你的问题,请参考以下文章
Swift开发教程--怎样自己定义TabBarItem的图片显示