截屏

发布于 2024-12-05 06:20:02 字数 762 浏览 1 评论 0原文

我正在尝试捕获(屏幕截图)视图。为此,我使用下面所示的一段代码,将其作为 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();

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

I am trying to capture (screen shot) a view. For that I am using a piece of code shown below that saves it to my document directory as a PNG image.

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();

Question: can I capture part of the view? Because in the above code I can't change the origin (frame). If anyone has other approach to capture a particular part of view please share it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

缪败 2024-12-12 06:20:02

您可以裁剪图像:
http://iosdevelopertips.com/graphics/how-to-crop- an-image.html

CGRect rect = CGRectMake(0,0,10,10);
CGImageRef imageRef = CGImageCreateWithImageInRect([screenshot CGImage], rect);
UIImage *croppedScreenshot = [UIImage imageWithCGImage:imageRef]; 
CGImageRelease(imageRef);

You could crop the image:
http://iosdevelopertips.com/graphics/how-to-crop-an-image.html

CGRect rect = CGRectMake(0,0,10,10);
CGImageRef imageRef = CGImageCreateWithImageInRect([screenshot CGImage], rect);
UIImage *croppedScreenshot = [UIImage imageWithCGImage:imageRef]; 
CGImageRelease(imageRef);
情仇皆在手 2024-12-12 06:20:02

试试这个代码。这确实有效,因为我已经在许多项目中实现了它:

- (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;
}

我希望这会对您有所帮助。

Try this code. This surely works as I have implemented it in many of my projects:

- (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;
}

I hope this will help you.

旧街凉风 2024-12-12 06:20:02

看看是否可以像这样指定矩形,然后截图。

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

您可以更改原点并查看它是否有效。
如果这不起作用,您可以尝试按照@mcb所述裁剪图像

See if you can specify the rect like this and then take screenshot.

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

You can alter the origin and see if it works.
If this doesn't work out, you can try cropping the image as mentioned by @mcb

离线来电— 2024-12-12 06:20:02

您可以使用此代码

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);

You can use this code

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文