首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何以编程方式掩蔽WKInterfaceImage

如何以编程方式掩蔽WKInterfaceImage
EN

Stack Overflow用户
提问于 2015-09-08 15:03:30
回答 1查看 233关注 0票数 1

我过去经常使用下面的代码来屏蔽UIImageView中的iOS

代码语言:javascript
复制
#import <QuartzCore/QuartzCore.h>

profilePhoto.layer.masksToBounds = YES;
profilePhoto.layer.cornerRadius = profilePhoto.bounds.size.width/2;

其结果是:

任何人都知道如何为WKInterfaceImage做同样的事情

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 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和修改只是一点点,使图像剪裁成一个圆圈。(我没有运行这段代码,所以可能会有一些问题。它应该能让你接近。)

代码语言:javascript
复制
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 imageClippedToCircle
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32461193

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档