[Objective-C] 如何制作一个圆角图像 →→→→→进入此内容的聊天室

来自 , 2020-11-26, 写在 Objective-C, 查看 110 次.
URL http://www.code666.cn/view/57342f6b
  1. void addRoundedRectToPath(CGContextRef context, CGRect rect, float ovalWidth, float ovalHeight);
  2. {
  3.         float fw, fh;
  4.         if (ovalWidth == 0 || ovalHeight == 0) {
  5.                 CGContextAddRect(context, rect);
  6.                 return;
  7.         }
  8.         CGContextSaveGState(context);
  9.         CGContextTranslateCTM (context, CGRectGetMinX(rect), CGRectGetMinY(rect));
  10.         CGContextScaleCTM (context, ovalWidth, ovalHeight);
  11.         fw = CGRectGetWidth (rect) / ovalWidth;
  12.         fh = CGRectGetHeight (rect) / ovalHeight;
  13.         CGContextMoveToPoint(context, fw, fh/2);
  14.         CGContextAddArcToPoint(context, fw, fh, fw/2, fh, 1);
  15.         CGContextAddArcToPoint(context, 0, fh, 0, fh/2, 1);
  16.         CGContextAddArcToPoint(context, 0, 0, fw/2, 0, 1);
  17.         CGContextAddArcToPoint(context, fw, 0, fw, fh/2, 1);
  18.         CGContextClosePath(context);
  19.         CGContextRestoreGState(context);
  20. }
  21.  
  22. - (UIImage *)roundCornersOfImage:(UIImage *)source;
  23. {
  24.         int w = source.size.width;
  25.         int h = source.size.height;
  26.  
  27.         CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
  28.         CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst);
  29.  
  30.         CGContextBeginPath(context);
  31.         CGRect rect = CGRectMake(0, 0, w, h);
  32.         addRoundedRectToPath(context, rect, 5, 5);
  33.         CGContextClosePath(context);
  34.         CGContextClip(context);
  35.  
  36.         CGContextDrawImage(context, CGRectMake(0, 0, w, h), source.CGImage);
  37.  
  38.         CGImageRef imageMasked = CGBitmapContextCreateImage(context);
  39.         CGContextRelease(context);
  40.         CGColorSpaceRelease(colorSpace);
  41.  
  42.         return [UIImage imageWithCGImage:imageMasked];    
  43. }
  44.  
  45.                                                
  46.  
  47. //objectc/142

回复 "如何制作一个圆角图像"

这儿你可以回复上面这条便签

captcha