iOS怎樣將圖片裁剪成圓形

這篇文章給大家分享的是有關(guān)iOS怎樣將圖片裁剪成圓形的內(nèi)容。小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧。

創(chuàng)新互聯(lián)專(zhuān)業(yè)為企業(yè)提供方城網(wǎng)站建設(shè)、方城做網(wǎng)站、方城網(wǎng)站設(shè)計(jì)、方城網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁(yè)設(shè)計(jì)與制作、方城企業(yè)網(wǎng)站模板建站服務(wù),十載方城做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。

原圖:

iOS怎樣將圖片裁剪成圓形

圓形圖片裁剪效果:

iOS怎樣將圖片裁剪成圓形

裁剪成帶邊框的圓形圖片:

iOS怎樣將圖片裁剪成圓形

核心代碼:

#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.開(kāi)啟跟原始圖片一樣大小的上下文
  UIGraphicsBeginImageContextWithOptions(image.size, NO, 0);
  //2.設(shè)置一個(gè)圓形裁剪區(qū)域
  //2.1繪制一個(gè)圓形
  UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
  //2.2.把圓形的路徑設(shè)置成裁剪區(qū)域
  [path addClip];//超過(guò)裁剪區(qū)域以外的內(nèi)容都給裁剪掉
  //3.把圖片繪制到上下文當(dāng)中(超過(guò)裁剪區(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.開(kāi)啟一個(gè)上下文
  CGSize size = CGSizeMake(image.size.width + 2 * borderW, image.size.height + 2 * borderW);
  UIGraphicsBeginImageContextWithOptions(size, NO, 0);
  //2.繪制大圓,顯示出來(lái)
  UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, size.width, size.height)];
  [borderColor set];
  [path fill];
  //3.繪制一個(gè)小圓,把小圓設(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)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!

分享題目:iOS怎樣將圖片裁剪成圓形
網(wǎng)站鏈接:http://muchs.cn/article46/ihghhg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站改版、商城網(wǎng)站網(wǎng)站導(dǎo)航、企業(yè)網(wǎng)站制作、移動(dòng)網(wǎng)站建設(shè)、App開(kāi)發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都做網(wǎng)站