這篇文章給大家分享的是有關(guān)iOS怎樣將圖片裁剪成圓形的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
創(chuàng)新互聯(lián)專業(yè)為企業(yè)提供方城網(wǎng)站建設(shè)、方城做網(wǎng)站、方城網(wǎng)站設(shè)計、方城網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計與制作、方城企業(yè)網(wǎng)站模板建站服務(wù),十載方城做網(wǎng)站經(jīng)驗,不只是建網(wǎng)站,更提供有價值的思路和整體網(wǎng)絡(luò)服務(wù)。
原圖:
圓形圖片裁剪效果:
裁剪成帶邊框的圓形圖片:
核心代碼:
#import <UIKit/UIKit.h> @interface UIImage (image) /** * 生成一張圓形圖片 * * @param image 要裁剪的圖片 * * @return 生成的圓形圖片 */ + (UIImage *)imageWithClipImage:(UIImage *)image; /** * 生成一張帶有邊框的圓形圖片 * * @param borderW 邊框?qū)挾? * @param borderColor 邊框顏色 * @param image 要添加邊框的圖片 * * @return 生成的帶有邊框的圓形圖片 */ + (UIImage *)imageWithBorder:(CGFloat)borderW color:(UIColor *)borderColor image:(UIImage *)image; @end
#import "UIImage+image.h" @implementation UIImage (image) + (UIImage *)imageWithClipImage:(UIImage *)image{ + //1.開啟跟原始圖片一樣大小的上下文 UIGraphicsBeginImageContextWithOptions(image.size, NO, 0); //2.設(shè)置一個圓形裁剪區(qū)域 //2.1繪制一個圓形 UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, image.size.width, image.size.height)]; //2.2.把圓形的路徑設(shè)置成裁剪區(qū)域 [path addClip];//超過裁剪區(qū)域以外的內(nèi)容都給裁剪掉 //3.把圖片繪制到上下文當(dāng)中(超過裁剪區(qū)域以外的內(nèi)容都給裁剪掉) [image drawAtPoint:CGPointZero]; //4.從上下文當(dāng)中取出圖片 UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); //5.關(guān)閉上下文 UIGraphicsEndImageContext(); return newImage; } + (UIImage *)imageWithBorder:(CGFloat)borderW color:(UIColor *)borderColor image:(UIImage *)image{ //1.開啟一個上下文 CGSize size = CGSizeMake(image.size.width + 2 * borderW, image.size.height + 2 * borderW); UIGraphicsBeginImageContextWithOptions(size, NO, 0); //2.繪制大圓,顯示出來 UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, size.width, size.height)]; [borderColor set]; [path fill]; //3.繪制一個小圓,把小圓設(shè)置成裁剪區(qū)域 UIBezierPath *clipPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(borderW, borderW, image.size.width, image.size.height)]; [clipPath addClip]; //4.把圖片繪制到上下文當(dāng)中 [image drawAtPoint:CGPointMake(borderW, borderW)]; //5.從上下文當(dāng)中取出圖片 UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); //6.關(guān)閉上下文 UIGraphicsEndImageContext(); return newImage; }
感謝各位的閱讀!關(guān)于“iOS怎樣將圖片裁剪成圓形”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
分享題目:iOS怎樣將圖片裁剪成圓形
網(wǎng)站鏈接:http://www.rwnh.cn/article46/ihghhg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站改版、商城網(wǎng)站、網(wǎng)站導(dǎo)航、企業(yè)網(wǎng)站制作、移動網(wǎng)站建設(shè)、App開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)