ios – 使用CGContextClip剪切带图像的圆圈

ios – 使用CGContextClip剪切带图像的圆圈,第1张

概述在我的应用程序中有一个imageViewA(框架{0,0,320,200}),我想用imageViewA的中心剪切一个圆,半径= 50,并绘制到另一个imageViewB(框架{0,0,100,100}); 像这样的原始图像效果: 并使用下面的代码来剪辑: UIGraphicsBeginImageContext(self.frame.size);CGContextRef ctx = UIGrap 在我的应用程序中有一个imageVIEwA(框架{0,320,200}),我想用imageVIEwA的中心剪切一个圆,半径= 50,并绘制到另一个imageVIEwB(框架{0,100,100});
像这样的原始图像效果:

并使用下面的代码来剪辑:

UIGraphicsBeginImageContext(self.frame.size);CGContextRef ctx = UIGraphicsGetCurrentContext();CGfloat height = self.bounds.size.height;CGContextTranslateCTM(ctx,0.0,height);CGContextScaleCTM(ctx,1.0,-1.0);CGContextAddArc(ctx,self.frame.size.wIDth/2,self.frame.size.height/2,50,2*M_PI,0);CGContextClosePath(ctx);CGContextSaveGState(ctx);CGContextClip(ctx);CGContextDrawImage(ctx,CGRectMake(0,self.frame.size.wIDth,self.frame.size.height),image.CGImage);CGContextRestoreGState(ctx);CGImageRef imageRef = CGBitmapContextCreateImage (ctx);UIImage *newImage = [UIImage imageWithCGImage:imageRef];Nsstring *headerPath = [Nsstring stringWithFormat:@"%@/header.png",HomeDirectory];NSData *imageData = UIImageJPEGRepresentation(newImage,1.0);if([imageData writetofile:headerPath atomically:YES]){    imageVIEwB.image = [UIImage imageWithContentsOffile:headerPath];}

剪辑图像效果如下:

我只需要圆形视图,但剪辑效果显示imageVIEwB有空白区域.
如何正确剪辑此图片?
谢谢!

解决方法
imageVIEwB.layer.cornerRadius = 50.0;imageVIEwB.layer.masksToBounds = YES;
总结

以上是内存溢出为你收集整理的ios – 使用CGContextClip剪切带图像的圆圈全部内容,希望文章能够帮你解决ios – 使用CGContextClip剪切带图像的圆圈所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1018626.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存