ios - 拍摄circle IOS的​​屏幕截图

标签 ios screenshot

我正在截取一些 UIButton 的屏幕截图,这些按钮是宽度约为 500、高度约为 500 的圆圈。由于屏幕截图只能以方形形式制作,并且我不想图像周围有白角,所以我使用了以下代码:

UIGraphicsBeginImageContext(self.Button3.layer.bounds.size);
[self.Button3.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage* image2 = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

    NSData* data = UIImagePNGRepresentation(image);
    [data writeToFile:BusinessCardPath atomically:YES];

这工作得很好,它返回给我一张只有圆圈而没有白角的照片。但它可以放大,因为它会丢失像素,并且它不包含在镜头中的所有元素,例如 UIButton 上的 UILabels,但不包含 UIButton 的 subview 。

如果我使用此代码:

CGSize imageSize = CGSizeMake(310, 310);

UIGraphicsBeginImageContext(self.Button3.layer.bounds.size);

CGContextRef context = UIGraphicsGetCurrentContext();
/*CGContextTranslateCTM(context, -5, -50);*/
// Iterate over every window from back to front
for (UIWindow *window in [[UIApplication sharedApplication] windows])
{
    if (![window respondsToSelector:@selector(screen)] || [window screen] == [UIScreen mainScreen])
    {
        // -renderInContext: renders in the coordinate space of the layer,
        // so we must first apply the layer's geometry to the graphics context
        CGContextSaveGState(context);
        // Center the context around the window's anchor point
        CGContextTranslateCTM(context, [window center].x, [window center].y);
        // Apply the window's transform about the anchor point
        CGContextConcatCTM(context, [window transform]);
        // Offset by the portion of the bounds left of and above the anchor point
        CGContextTranslateCTM(context,
                              -[window bounds].size.width * [[window layer] anchorPoint].x,
                              -[window bounds].size.height * [[window layer] anchorPoint].y);

        // Render the layer hierarchy to the current context
        [[window layer] renderInContext:context];

        // Restore the context
        CGContextRestoreGState(context);
    }
}

// Retrieve the screenshot image
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

...我得到了相反的效果。上面的元素包含在高分辨率的屏幕截图中,但图像周围有白色的角。

最佳答案

您可以使用 CAShapeLayer 来遮盖图像的某些部分。 .path 属性用于定义将渲染的区域。

UIImageView *myImageView = [UIImageView alloc] init];
myImageView.image = [UIImage imageNamed:@"anImage.gif"];
[myImageView setClipsToBounds:YES];

UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:myImageView.bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(48, 48)];
CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.frame = myImageView.bounds;
maskLayer.path = maskPath.CGPath;

myImageView.layer.mask = maskLayer;

这应该会给你一个圆形图像。您可能需要使用半径。

关于ios - 拍摄circle IOS的​​屏幕截图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18514778/

相关文章:

android - PWA Vue JS App 不显示在设备上

iphone - 拖动特定的 UIImageView

python - Python 中 Selenium Webdriver 测试失败时的自动截图

iphone - iPhone游戏的游戏中心集成?

iphone - objective-c 中的字符串操作

ios - 将我的应用程序放在 App Store 上的 "pre-order"

c# - 如何使用 C# 和 ASP.NET 从网页截取 div 的屏幕截图?

安卓 : taking Screenshot of the selected area programmatically

VBA 调整屏幕分辨率

ios - 特定 CGRect 中的 UIGraphicsGetImageFromCurrentImageContext