ios 裁剪出一個圓形的頭像,這樣的例子在簡書已經很多了,我就不詳細的介紹了.今天我想講的是怎麼裁剪出一個環形的圖片.廢話不多說,見代碼。
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.設置裁剪區域 [pathaddClip]; // 4.畫圖 [selfdrawAtPoint:CGPointZero]; // 5.取出圖片 UIImage*image =UIGraphicsGetImageFromCurrentImageContext(); // 6.關閉上下文 UIGraphicsEndImageContext(); returnimage; }
2:用CGContextClearRect 的功能 制作環形圖片
- (UIImage*)getClearRectImage:(UIImage*)image{ UIGraphicsBeginImageContextWithOptions(image.size,NO,0.0f); CGContextRefctx =UIGraphicsGetCurrentContext(); //默認繪制的內容尺寸和圖片一樣大,從某一點開始繪制 [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則是由中心點取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; }
當然這是就用到了UIimage的size的屬性了.
CGSize size = yuanlai.size; float imageSize; NSLog(@"size==height%f====width%f",size.height,size.width); if(size.height>= size.width) { imageSize = size.width; }else{ imageSize = size.height; }