拍摄circle IOS的屏幕截图

拍摄circle IOS的屏幕截图,ios,screenshot,Ios,Screenshot,我正在拍摄一些UIButtons的屏幕截图,它们是大约500宽500高的圆圈。由于屏幕截图只能是方形的,而且我不希望图像周围有白色的角落,所以我使用了以下代码: UIGraphicsBeginImageContext(self.Button3.layer.bounds.size); [self.Button3.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage* image2 = UIGraphicsGetImageFrom

我正在拍摄一些UIButtons的屏幕截图,它们是大约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];
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();
这是完美的作品,它返回给我的照片只有一个没有白色角落的圆圈。但是它可以被放大,因为它会丢失像素,并且它不会在快照中包含所有元素,例如UIButton上的UILabel,但不会包含UIButton的子视图

如果我使用此代码:

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

    NSData* data = UIImagePNGRepresentation(image);
    [data writeToFile:BusinessCardPath atomically:YES];
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;
这会给你一个圆形的图像。您可能需要使用半径