iOS 畫圓形圖像的幾種方法

首先聲明一個UIImageView

@property (nonatomic, strong) UIImageView *cycleImv;

方法一:

self.cycleImv= [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 50, 50)];

[self.view addSubview:self.cycleImv];

// 為圖片切圓

self.cycleImv.layer.masksToBounds = YES;

self.cycleImv.layer.cornerRadius = self.cycleImv.frame.size.width / 2.0;

// 為圖片添加邊框,根據需要設置邊框

self.cycleImv.layer.borderWidth = 2.0;//邊框的寬度

self.cycleImv.layer.borderColor = [UIColor redColor].CGColor;//邊框的顏色

方法二:

- (void)drawRect:(CGRect)rect

{

UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.cycleImv.bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:self.cycleImv.bounds.size];

CAShapeLayer *maskLayer = [[CAShapeLayer alloc]init];

//設置大小

maskLayer.frame = self.cycleImv.bounds;

//設置圖形樣子

maskLayer.path = maskPath.CGPath;

self.cycleImv.layer.mask = maskLayer;

}

方法三:

將網絡圖片裁剪為圓形,首先建立一個UIImage分類UIImage+Extension,一個UIImageView分類UIImageView+CircularImv。

UIImage+Extension.h文件

#import@interface UIImage (Extension)

- (UIImage *)circleImage;

@end

UIImage+Extension.m文件

#import "UIImage+Extension.h"

@implementation UIImage (Extension)

- (UIImage *)circleImage

{

// 開始圖形上下文,NO代表透明

UIGraphicsBeginImageContextWithOptions(self.size, NO, 0.0);

// 獲得圖形上下文

CGContextRef ctx = UIGraphicsGetCurrentContext();

// 設置一個范圍

CGRect rect = CGRectMake(0, 0, self.size.width, self.size.height);

// 根據一個rect創建一個橢圓

CGContextAddEllipseInRect(ctx, rect);

// 裁剪

CGContextClip(ctx);

// 將原照片畫到圖形上下文

[self drawInRect:rect];

// 從上下文上獲取剪裁后的照片

UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();

// 關閉上下文

UIGraphicsEndImageContext();

return newImage;

}

@end


使用了SDWebImage加載網絡圖片,所以加上UIImageView+WebCache.h頭文件。

UIImageView+CircularImv.h文件

#import@interface UIImageView (CircularImv)

- (void)setCircularImvURL:(NSString *)imageUrl holderImageName:(NSString *)imageName;

@end

UIImageView+CircularImv.m文件

#import "UIImageView+CircularImv.h"

#import "UIImageView+WebCache.h"

#import "UIImage+Extension.h"

@implementation UIImageView (CircularImv)

- (void)setCircularImvURL:(NSString *)imageUrl holderImageName:(NSString *)imageName

{

//占位圖片,當URL上下載的圖片為空,就顯示該圖片

UIImage *placeholder = [[UIImage imageNamed:imageName] circleImage];

[self sd_setImageWithURL:[NSURL URLWithString:imageUrl] placeholderImage:placeholder completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {

//當圖片下載完會來到這個block,執行以下代碼

self.image = image ? [image circleImage] : placeholder;

}];

}

@end


使用方法:

[self.cycleImv setCircularImvURL:網絡圖片地址 holderImageName:本地占位圖片名稱];

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 在iOS中隨處都可以看到絢麗的動畫效果,實現這些動畫的過程并不復雜,今天將帶大家一窺ios動畫全貌。在這里你可以看...
    每天刷兩次牙閱讀 8,573評論 6 30
  • 在iOS中隨處都可以看到絢麗的動畫效果,實現這些動畫的過程并不復雜,今天將帶大家一窺iOS動畫全貌。在這里你可以看...
    F麥子閱讀 5,147評論 5 13
  • 1、禁止手機睡眠[UIApplication sharedApplication].idleTimerDisabl...
    DingGa閱讀 1,144評論 1 6
  • 1.利用xib建立圓角圖片: 只需要在xib中選擇你要弄成圓角的控件,按照圖片中那樣設置就可以。(避免輸入錯誤,建...
    怪獸密保閱讀 2,400評論 6 1
  • 生,如一場夢。 帶來那所謂的,悲傷,煩惱,憂愁的夜。 卻又讓那黎明,沖散這一切。 白天,黑夜,交織著生活的悲與喜。...
    無言心閱讀 206評論 0 0