Swift開發教程--有關圖片處理的一些有用函數

來源:互聯網
上載者:User

標籤:swift   開發   教程   代碼   ios   

/**

    *  切圓形圖片

    *

    *  @param image:UIImage

    *  @param inset:CGFloat

    *

    *  @return UIImage

    */

    classfunc 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

    */

    classfunc 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

    */

    classfunc 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

    */

    classfunc 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

    */

    classfunc saveImageFile(image:UIImage,filename:NSString) {

        var path:NSString =NSHomeDirectory().stringByAppendingPathComponent("Documents").stringByAppendingPathComponent(filenameas String);

        UIImagePNGRepresentation(image).writeToFile(pathas String, atomically:true);

    }

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

Swift開發教程--有關圖片處理的一些有用函數

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.