圖片框圓角處理(UIImageView):
添加QuartzCore.framework
導入庫頭文件
#import “QuartzCore/QuartzCore.h”
//圓角設置 imageView.layer.cornerRadius
= 8;(值越大,角就越圓) imageView.layer.masksToBounds
= YES; //邊框寬度及顏色設置 [imageView.layer
setBorderWidth:2]; [imageView.layer
setBorderColor:[UIColor blueColor]]; //設置邊框爲藍色 //自動適應,保持圖片寬高比 imageView.contentMode
= UIViewContentModeScaleAspectFit;
還有一種方法
寫成一個類
Phone上的程序在顯示頭像之類的圖片時,基本上都是使用圓角的方式,比如TweetBot這個評價最高的Twitter客戶端。stackoverflow上也有很多關於如何實現圓角圖片的問題,實現方法有不少,不過最簡單的實現是使用CALayer CALayer *roundedLayer = [self layer]; [roundedLayer setMasksToBounds:YES]; roundedLayer.cornerRadius = 5.0; roundedLayer.borderColor = [[UIColor grayColor] CGColor]; 其中的cell是因爲圖片在UITableView中使用。但是如果要在每次想要使用圓角圖片的時候,都要寫上這麼寫代碼的話,就不符合碼農懶的特性。既然iOS沒有提供圓角的UIImageView,那麼我們就自己來創建一個UIRoundedImageView。那麼把這段代碼加到哪裏呢?這裏, (void)willMoveToWindow:(UIWindow *)newWindow { CALayer *roundedLayer = [self layer]; [roundedLayer setMasksToBounds:YES]; roundedLayer.cornerRadius = 5.0; roundedLayer.borderColor = [[UIColor grayColor] CGColor]; } 之後再把UIImageView控件的Custom Class設置爲UIRoundedImageView |