iOS-圓角圖片,下載網(wǎng)絡(luò)上的圖片并顯示成圓形

1.利用xib建立圓角圖片:

只需要在xib中選擇你要弄成圓角的控件,按照?qǐng)D片中那樣設(shè)置就可以。(避免輸入錯(cuò)誤,建議復(fù)制layer.cornerRadiuslayer.masksToBounds

2.在代碼中設(shè)置

在代碼中設(shè)置和在xib中設(shè)置的道理是一樣的。在ViewController里面關(guān)聯(lián)xib中的控件,然后設(shè)置其屬性值;(如UIImageView類)

self.profileImageView.layer.cornerRadius=5.0;//圓角的半徑,一般設(shè)置成5

self.profileImageView.layer.masksToBounds=YES;//這個(gè)屬性需設(shè)置成YES,否則圖片不能生效。

3創(chuàng)建圓形用戶頭像

接下來,讓我們看看如何通過改變圓角半徑,使用戶頭像轉(zhuǎn)換成一個(gè)圓形圖像。和2中寫的類似,需要改變的是圓形半徑。其半徑是其長度的一半。(圖片需是正方形圖片)

self.profileImageView.layer.cornerRadius = self.profileImageView.frame.size.width / 2

self.profileImageView.clipsToBounds = YES;

4為圖片添加邊框

在設(shè)置圓角半徑的代碼后面加入以下兩行代碼:

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

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

5可以給UIImage添加一個(gè)分類UIImage+Extension

分類中增加一個(gè)返回圓形圖片的方法,擴(kuò)展性強(qiáng)(如果很多地方需要用到圓形圖片,如tableViewCell上的用戶頭像,以上的方法就會(huì)讓程序變得很卡,推薦使用下面的方法)

#import <UIKit/UIKit.h>

@interface UIImage (Extension)

- (UIImage *)circleImage;

@end

#import "UIImage+Extension.h"

@implementation UIImage (Extension)

- (UIImage *)circleImage {

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

UIGraphicsBeginImageContextWithOptions(self.size, NO, 0.0);

// 獲得圖形上下文

CGContextRef ctx = UIGraphicsGetCurrentContext();

// 設(shè)置一個(gè)范圍

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

// 根據(jù)一個(gè)rect創(chuàng)建一個(gè)橢圓

CGContextAddEllipseInRect(ctx, rect);

// 裁剪

CGContextClip(ctx);

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

[self drawInRect:rect];

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

UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();

// 關(guān)閉上下文

UIGraphicsEndImageContext();

return newImage;

}

或者這樣:

- (UIImage*)cropImageWithRect:(CGRect)cropRect

{

CGRect drawRect = CGRectMake(-cropRect.origin.x , -cropRect.origin.y, self.size.width * self.scale, self.size.height * self.scale);

UIGraphicsBeginImageContext(cropRect.size);

CGContextRef context = UIGraphicsGetCurrentContext();

CGContextClearRect(context, CGRectMake(0, 0, cropRect.size.width, cropRect.size.height));

[self drawInRect:drawRect];

UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return image;

}

具體使用:

// 獲得的就是一個(gè)圓形的圖片

UIImage *placeHolder = [[UIImage imageNamed:@"defaultUserIcon"] circleImage];

需求案例-進(jìn)一步應(yīng)用

請(qǐng)求獲取網(wǎng)絡(luò)圖片顯示到UIImageView上,并把用戶圖片改成圓形。

這里需要用到SDWebImage框架。新建UIImageView分類,并導(dǎo)入頭文件UIImageView+WebCache.h

--------.h文件

#import <UIKit/UIkit.h>

@interface UIImageView (Extension)

- (void)setHeader:(NSString *)url;

@end

-----.m文件

#import "UIImageView+XMGExtension.h"

#import <UIImageView+WebCache.h>

@implementation UIImageView (Extension)

- (void)setHeader:(NSString *)url

{

UIImage *placeholder = [[UIImage imageNamed:@"defaultUserIcon"] circleImage];//占位圖片,當(dāng)URL上下載的圖片為空,就顯示該圖片

[self sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:placeholder completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {//當(dāng)圖片下載完會(huì)來到這個(gè)block,執(zhí)行以下代碼

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

}];

}

@end

調(diào)用方式

// 設(shè)置頭像,把圖片的URL傳過去

[self.profileImageView setHeader:profile_image];

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • 在iOS中隨處都可以看到絢麗的動(dòng)畫效果,實(shí)現(xiàn)這些動(dòng)畫的過程并不復(fù)雜,今天將帶大家一窺ios動(dòng)畫全貌。在這里你可以看...
    每天刷兩次牙閱讀 8,572評(píng)論 6 30
  • 在iOS中隨處都可以看到絢麗的動(dòng)畫效果,實(shí)現(xiàn)這些動(dòng)畫的過程并不復(fù)雜,今天將帶大家一窺iOS動(dòng)畫全貌。在這里你可以看...
    F麥子閱讀 5,141評(píng)論 5 13
  • 1、設(shè)置UILabel行間距 NSMutableAttributedString* attrString = [[...
    十年一品溫如言1008閱讀 1,720評(píng)論 0 3
  • *7月8日上午 N:Block :跟一個(gè)函數(shù)塊差不多,會(huì)對(duì)里面所有的內(nèi)容的引用計(jì)數(shù)+1,想要解決就用__block...
    炙冰閱讀 2,547評(píng)論 1 14
  • 文| 黎露 蘭臺(tái)讀書 今日采訪健身達(dá)人:黎露,健身達(dá)人,通過健身,從160斤減到116斤。 接下來我們看一下黎露怎...
    藍(lán)楓123閱讀 5,172評(píng)論 25 76