/**
* 切圆形图片
*
* @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
只要是继承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);
}
原文链接:https://www.f2er.com/swift/326314.html