国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

iOSUIImage剪切圓形

2019-11-14 18:30:10
字體:
來源:轉載
供稿:網友

 

//.h文件

#import <UIKit/UIKit.h>

 

@interface UIImage (XG)

/**

 *  @param icon         頭像圖片名稱

 *  @param borderImage  邊框的圖片名稱

 *  @param border       邊框大小

 *

 *  @return 圓形的頭像圖片

 */

+ (instancetype)imageWithIconName:(NSString *)icon borderImage:(NSString *)borderImage border:(int)border;

@end

 

//.m文件

#import "UIImage+XG.h"

 

@implementation UIImage (XG)

+ (instancetype)imageWithIconName:(NSString *)icon borderImage:(NSString *)borderImage border:(int)border{

    //頭像圖片

    UIImage * image = [UIImage imageNamed:icon];

    //邊框圖片

    UIImage * borderImg = [UIImage imageNamed:borderImage];

    //

    CGSize size = CGSizeMake(image.size.width + border, image.size.height + border);

    

    //創建圖片上下文

    UIGraphicsBeginImageContextWithOptions(size, NO, 0);

    

    //繪制邊框的圓

    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextAddEllipseInRect(context, CGRectMake(0, 0, size.width, size.height));

    

    //剪切可視范圍

    CGContextClip(context);

    

    //繪制邊框圖片

    [borderImg drawInRect:CGRectMake(0, 0, size.width, size.height)];

    

    //設置頭像frame

    CGFloat iconX = border / 2;

    CGFloat iconY = border / 2;

    CGFloat iconW = image.size.width;

    CGFloat iconH = image.size.height;

    

    //繪制圓形頭像范圍

    CGContextAddEllipseInRect(context, CGRectMake(iconX, iconY, iconW, iconH));

    

    //剪切可視范圍

    CGContextClip(context);

    

    //繪制頭像

    [image drawInRect:CGRectMake(iconX, iconY, iconW, iconH)];

    

    //取出整個圖片上下文的圖片

    UIImage *iconImage = UIGraphicsGetImageFromCurrentImageContext();

    

    return iconImage;

}

@end

  borderImage 是邊框  不需要的話給nil就可以

  border   是邊框寬度 不需要的話給0就行了

UIImage * image = [UIImage imageWithIconName:@"頭像.png" borderImage:nil border:0];

 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 沙雅县| 桃江县| 新疆| 房产| 伊宁县| 砚山县| 涪陵区| 新乡县| 顺昌县| 隆林| 建阳市| 尼勒克县| 方山县| 武宁县| 松原市| 江都市| 鄂托克前旗| 玉树县| 习水县| 青岛市| 瓦房店市| 曲阳县| 福州市| 南涧| 获嘉县| 永川市| 孝义市| 克什克腾旗| 榆中县| 磴口县| 沙洋县| 嵊州市| 崇阳县| 乡城县| 云梦县| 隆林| 建平县| 仁寿县| 象山县| 岱山县| 南汇区|