怎么裁剪出一個(gè)環(huán)形圖片 (iOS實(shí)現(xiàn)圓形圖片)

ios 裁剪出一個(gè)圓形的頭像,,這樣的例子在簡書已經(jīng)很多了,我就不詳細(xì)的介紹了.今天我想講的是怎么裁剪出一個(gè)環(huán)形的圖片.廢話不多說:見代碼:



1:首先將一張圖片裁剪成圓形圖片,,

/**圓形圖片裁剪*/

- (UIImage *)wjf_circleImage?

{

//利用self生成一張圓形圖片

// 1.開啟圖形上下文

UIGraphicsBeginImageContextWithOptions(self.size,NO,0);

// 2.描述圓形路徑

UIBezierPath*path = [UIBezierPathbezierPathWithOvalInRect:CGRectMake(0,0,self.size.width,self.size.height)];

// 3.設(shè)置裁剪區(qū)域

[pathaddClip];

// 4.畫圖

[selfdrawAtPoint:CGPointZero];

// 5.取出圖片

UIImage*image =UIGraphicsGetImageFromCurrentImageContext();

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

UIGraphicsEndImageContext();

returnimage;

}

2:用CGContextClearRect 的功能 制作環(huán)形圖片

- (UIImage*)getClearRectImage:(UIImage*)image{

UIGraphicsBeginImageContextWithOptions(image.size,NO,0.0f);

CGContextRefctx =UIGraphicsGetCurrentContext();

//默認(rèn)繪制的內(nèi)容尺寸和圖片一樣大,從某一點(diǎn)開始繪制

[imagedrawAtPoint:CGPointZero];

CGFloatbigRaduis = image.size.width/5;

CGRectcirleRect =CGRectMake(image.size.width/2-bigRaduis, image.size.height/2-bigRaduis, bigRaduis*2, bigRaduis*2);

//CGContextAddArc(ctx,image.size.width/2-bigRaduis,image.size.height/2-bigRaduis, bigRaduis, 0.0, 2*M_PI, 0);

CGContextAddEllipseInRect(ctx,cirleRect);

CGContextClip(ctx);

CGContextClearRect(ctx,cirleRect);

UIImage*newImage =UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

returnnewImage;

}

如果你的圖片是正方形的話,就大功告成了,但是你的圖片是長方形呢???不用怕.

注:將長方形圖片變成正方形圖片:

-(UIImage*)getSquareImage:(UIImage

*)image RangeCGRect:(CGRect)range

centerBool:(BOOL)centerBool{

/*如若centerBool為Yes則是由中心點(diǎn)取mCGRect范圍的圖片*/

floatimgWidth = image.size.width;

floatimgHeight = image.size.height;

floatviewWidth =range.size.width;

floatviewHidth =range.size.height;

CGRectrect;

if(centerBool)

rect =CGRectMake((imgWidth-viewWidth)/2,

(imgHeight-viewHidth)/2,viewWidth,viewHidth);

else{

if(viewHidth

{

if(imgWidth<= imgHeight){

rect=CGRectMake(0,0,imgWidth, imgWidth*viewHidth/viewWidth);

}else{

floatwidth = viewWidth*imgHeight/viewHidth;

floatx = (imgWidth - width)/2;

if(x >0){

rect =CGRectMake(x,0, width, imgHeight);

}else{

rect=CGRectMake(0,0,imgWidth, imgWidth*viewHidth/viewWidth);

}

}

}else{

if(imgWidth<= imgHeight){

floatheight = viewHidth*imgWidth/viewWidth;

if(height < imgHeight){

rect =CGRectMake(0,0,imgWidth, height);

}else

{

rect =CGRectMake(0,0,viewWidth*imgHeight/viewHidth,imgHeight);

}

}else

{

floatwidth = viewWidth*imgHeight/viewHidth;

if(width < imgWidth)

{

floatx = (imgWidth - width)/2;

rect =CGRectMake(x,0,width, imgHeight);

}else

{

rect =CGRectMake(0,0,imgWidth, imgHeight);

}

}

}

}

CGImageRefSquareImageRef =

CGImageCreateWithImageInRect(image.CGImage,rect);

CGRectSquareImageBounds =CGRectMake(0,0,CGImageGetWidth(SquareImageRef),CGImageGetHeight(SquareImageRef));

UIGraphicsBeginImageContext(SquareImageBounds.size);

CGContextRefcontext =UIGraphicsGetCurrentContext();

CGContextDrawImage(context,SquareImageBounds,SquareImageRef);

UIImage* SquareImage = [UIImageimageWithCGImage:SquareImageRef];

UIGraphicsEndImageContext();

returnSquareImage;

}

當(dāng)然這是就用到了UIimage的size的屬性了.

CGSize size =yuanlai.size;

floatimageSize;

NSLog(@"size==height%f====width%f",size.height,size.width);

if(size.height>= size.width) {

imageSize = size.width;

}else{

imageSize = size.height;

}

有什么不對的地方,大家都提提意見...分享了一關(guān)于:繪圖詳解的文章:http://m.blog.csdn.net/article/details?id=51251717

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容