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

    只要是继承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);

    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值