我过去经常使用下面的代码来屏蔽UIImageView中的iOS
#import <QuartzCore/QuartzCore.h>
profilePhoto.layer.masksToBounds = YES;
profilePhoto.layer.cornerRadius = profilePhoto.bounds.size.width/2;其结果是:
任何人都知道如何为WKInterfaceImage做同样的事情
发布于 2015-09-08 23:03:44
您将需要使用Core将您的图像绘制成一个圆,然后将该图像设置为WKInterfaceImage。有关如何使用绘制具有圆形地图的图像,请参阅此帖子。How to mask a square image into an image with round corners in the iPhone SDK?
这里是从另一个复制的代码,所以post和修改只是一点点,使图像剪裁成一个圆圈。(我没有运行这段代码,所以可能会有一些问题。它应该能让你接近。)
static void addRoundedRectToPath(CGContextRef context, CGRect rect, float ovalWidth, float ovalHeight)
{
float fw, fh;
if (ovalWidth == 0 || ovalHeight == 0) {
CGContextAddRect(context, rect);
return;
}
CGContextSaveGState(context);
CGContextTranslateCTM (context, CGRectGetMinX(rect), CGRectGetMinY(rect));
CGContextScaleCTM (context, ovalWidth, ovalHeight);
fw = CGRectGetWidth (rect) / ovalWidth;
fh = CGRectGetHeight (rect) / ovalHeight;
CGContextMoveToPoint(context, fw, fh/2);
CGContextAddArcToPoint(context, fw, fh, fw/2, fh, 1);
CGContextAddArcToPoint(context, 0, fh, 0, fh/2, 1);
CGContextAddArcToPoint(context, 0, 0, fw/2, 0, 1);
CGContextAddArcToPoint(context, fw, 0, fw, fh/2, 1);
CGContextClosePath(context);
CGContextRestoreGState(context);
}
UIImage* img = <My_Image_To_Draw?
UIGraphicsBeginImageContextWithOptions(img.size, NO, 2.0);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
CGRect rect = CGRectMake(0,0,img.size.width, img.size.height);
addRoundedRectToPath(context, rect, img.size.width, img.size.height);
CGContextClip(context);
[image drawInRect:rect];
UIImage* imageClippedToCircle = UIGraphicsGetImageFromCurrentImageContext();
CGContextRestoreGState(context);
//Now you can use imageClippedToCirclehttps://stackoverflow.com/questions/32461193
复制相似问题