捕捉屏幕

7

我正在尝试捕获(截屏)一个视图。为此,我使用下面展示的一段代码将其保存为PNG图像到我的文档目录中。

UIGraphicsBeginImageContextWithOptions(highlightViewController.fhView.centerView.frame.size, YES, 1.0);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext();
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:@"1.png"];
NSData *imageData = UIImagePNGRepresentation(screenshot);
[imageData writeToFile:appFile atomically:YES];
UIGraphicsEndImageContext();

问题:我能够捕获视图的一部分吗?因为在上面的代码中我不能更改原点(框架)。如果有其他方法可以捕获特定部分的视图,请分享。


你想仅从代码中捕获吗?还是有一些快捷方式可以为您工作? - Nitish
感谢您的回复,我想仅使用代码进行捕获。 - ajay
4个回答

6

1

尝试这段代码。我在许多我的项目中实现了它,它肯定可以正常工作:

- (UIImage *)image
{
    if (cachedImage == nil) {
        //YOU CAN CHANGE THE FRAME HERE TO WHATEVER YOU WANT TO CAPTURE
        CGRect imageFrame = CGRectMake(0, 0, 400, 300); 
        UIView *imageView = [[UIView alloc] initWithFrame:imageFrame];
        [imageView setOpaque:YES];
        [imageView setUserInteractionEnabled:NO];

        [self renderInView:imageView withTheme:nil];        

        UIGraphicsBeginImageContext(imageView.bounds.size);
            CGContextRef c = UIGraphicsGetCurrentContext();
            CGContextGetCTM(c);
            CGContextScaleCTM(c, 1, -1);
            CGContextTranslateCTM(c, 0, -imageView.bounds.size.height);
            [imageView.layer renderInContext:c];
            cachedImage = [UIGraphicsGetImageFromCurrentImageContext() retain];

            // rescale graph
            UIImage* bigImage = UIGraphicsGetImageFromCurrentImageContext();
            CGImageRef scaledImage = [self newCGImageFromImage:[bigImage CGImage] scaledToSize:CGSizeMake(100.0f, 75.0f)];
            cachedImage = [[UIImage imageWithCGImage:scaledImage] retain];
            CGImageRelease(scaledImage);
        UIGraphicsEndImageContext();

        [imageView release];
    }

    return cachedImage;
}

希望这能对您有所帮助。


0

你可以使用这段代码

UIGraphicsBeginImageContext(self.view.bounds.size);

[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];

UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGRect rect;
rect = CGRectMake(250,61 ,410, 255);
CGImageRef imageRef = CGImageCreateWithImageInRect([viewImage CGImage], rect);

UIImage *img = [UIImage imageWithCGImage:imageRef]; 

UIImageWriteToSavedPhotosAlbum(img, nil, nil, nil); 
CGImageRelease(imageRef);

0

看看你能否像这样指定矩形,然后截屏。

CGRect requiredRect = CGRectMake(urView.frame.origin.x, urView.frame.origin.y, urView.bounds.size.width, urView.bounds.size.height);
UIGraphicsBeginImageContext(requiredRect.size);

你可以更改来源并查看是否有效。 如果这不起作用,您可以尝试像@mcb提到的那样裁剪图像


嗨,这个问题是关于裁剪还是捕获的。虽然我认为它是关于捕获图像的,也许你的看法与我不同。但无论如何都是好答案 :) - Parth Bhatt
@Parth Bhatt:实际上,裁剪是另一个选项。就这些。 - visakh7
谢谢你的回答,我想在视图中的特定部分进行屏幕截图。 - ajay
@ajay:请查看我的答案,我认为那就是你要找的。如果你有任何疑问,请联系我! - Parth Bhatt

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接