gpt4 book ai didi

ios - 截取 UIView iOS 的屏幕截图

转载 作者:行者123 更新时间:2023-11-29 02:56:45 29 4
gpt4 key购买 nike

我想截取UIView(该 View 将包含签名)并将其保存到应用程序文件中的本地文件中,以便以后调用图像点显示在类似于 UIImageView 的内容中。下面是签名 UIView 背后的代码。

#import "NISignatureViewQuartz.h"
#import <QuartzCore/QuartzCore.h>

@implementation NISignatureViewQuartz

UIBezierPath *path;

- (void)commonInit
{
path = [UIBezierPath bezierPath];

// Capture touches
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
pan.maximumNumberOfTouches = pan.minimumNumberOfTouches = 1;
[self addGestureRecognizer:pan];

// Erase with long press
[self addGestureRecognizer:[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(erase)]];

}

- (id)initWithCoder:(NSCoder *)aDecoder
{
if (self = [super initWithCoder:aDecoder]) [self commonInit];
return self;
}

- (id)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) [self commonInit];
return self;
}

- (void)erase
{
path = [UIBezierPath bezierPath];
[self setNeedsDisplay];
}

- (void)pan:(UIPanGestureRecognizer *)pan {
CGPoint currentPoint = [pan locationInView:self];

if (pan.state == UIGestureRecognizerStateBegan) {
[path moveToPoint:currentPoint];
} else if (pan.state == UIGestureRecognizerStateChanged)
[path addLineToPoint:currentPoint];

[self setNeedsDisplay];
}

- (void)drawRect:(CGRect)rect
{
[[UIColor blackColor] setStroke];
[path stroke];
}

@end

我该怎么做?

最佳答案

您想将 View 的图层渲染到图形上下文中。这非常简单。在您的 NISignatureViewQuartz 类中,您可以添加此方法:

- (UIImage *)snapshot {
UIGraphicsBeginImageContext(self.frame.size);
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}

关于ios - 截取 UIView iOS 的屏幕截图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23785817/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com