生成圆角图片


/ Published in: Objective C
Save to your folder(s)



Copy this code and paste it in your HTML
  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.  

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.