Swift开发教程--有关图片处理的一些有用函数

前端之家收集整理的这篇文章主要介绍了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,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,image.size.width*scaleSize,image.size.height*scaleSize));

var scaledImage:UIImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return scaledImage;

}

/**

* 3.处理某个特定View

只要是继承UIViewobject 都可以处理

必须先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相关文章