亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

iOS如何將圖片裁剪成圓形

 更新時(shí)間:2020年02月22日 09:20:55   作者:LayneCheung  
這篇文章主要為大家詳細(xì)介紹了iOS如何將圖片裁剪成圓形,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了iOS將圖片裁剪成圓形的具體代碼,供大家參考,具體內(nèi)容如下

原圖:

圓形圖片裁剪效果:

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

核心代碼:

#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è)置一個(gè)圓形裁剪區(qū)域
  //2.1繪制一個(gè)圓形
  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.開啟一個(gè)上下文
  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.繪制一個(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;
}

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論