截取UIView iOS的截图

时间:2014-05-21 14:14:05

标签: ios objective-c uiview

我想截取UIView的截图(视图将包含签名)并将其保存到应用程序文件中的本地文件中,以便稍后可以调用该图像以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

我将如何做到这一点?

3 个答案:

答案 0 :(得分:1)

您希望将视图的图层渲染到图形上下文中。这非常简单。在NISignatureViewQuartz课程中,您可以添加此方法:

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

答案 1 :(得分:0)

我写了一个有用的帮助类来获取和管理屏幕截图:

@implementation MGImageHelper

/* Get the screenshot of an UIView (so take just UIKit elements and not OpenGL or AVFoundation stuff. */
+ (UIImage *)getScreenshotFromView:(UIView *)captureView
{
    CGRect rect = [captureView bounds];
    UIGraphicsBeginImageContextWithOptions(rect.size,YES,0.0f);

    CGContextRef context = UIGraphicsGetCurrentContext();
    [captureView.layer renderInContext:context];
    UIImage *capturedImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return capturedImage;
}

/* Get the screenshot of a determinate rect of an UIView, and scale it to the size that you want. */
+ (UIImage *)getScreenshotFromView:(UIView *)captureView withRect:(CGRect)captureRect andScaleToSize:(CGSize)newSize
{
    UIImage *image = [[self class] getScreenshotFromView:captureView];
    image = [[self class] cropImage:image withRect:captureRect];
    image = [[self class] scaleImage:image toSize:newSize];

    return image;
}

/* Get the screenshot of the screen (useful when you have UIKit elements and OpenGL or AVFoundation stuff */
+ (UIImage *)screenshotFromScreen
{
    CGImageRef UIGetScreenImage(void);
    CGImageRef screen = UIGetScreenImage();
    UIImage* screenImage = [UIImage imageWithCGImage:screen];
    CGImageRelease(screen);

    return screenImage;
}

/* Get the screenshot of a determinate rect of the screen, and scale it to the size that you want. */
+ (UIImage *)getScreenshotFromScreenWithRect:(CGRect)captureRect andScaleToSize:(CGSize)newSize
{
    UIImage *image = [[self class] screenshotFromScreen];
    image = [[self class] cropImage:image withRect:captureRect];
    image = [[self class] scaleImage:image toSize:newSize];

    return image;
}


/* Methods used from methods above but also usable in singular */

+ (UIImage *)cropImage:(UIImage *)image withRect:(CGRect)rect
{
    CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], rect);
    UIImage *cropedImage = [UIImage imageWithCGImage:imageRef];

    return cropedImage;
}

+ (UIImage *)scaleImage:(UIImage *)image toSize:(CGSize)newSize
{
    UIGraphicsBeginImageContextWithOptions(newSize, YES, 0.0);
    [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
    UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return scaledImage;
}

@end

答案 2 :(得分:0)

您可以使用从iOS 7开始提供的UIView方法,专门为此设计:

- (BOOL)drawViewHierarchyInRect:(CGRect)rect afterScreenUpdates:(BOOL)afterUpdates;

e.g。

UIGraphicsBeginImageContext(self.bounds.size);
[self drawViewHierarchyInRect:self.bounds afterScreenUpdates:NO];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();