ios 如何捕获屏幕的特定部分

发布于 2024-12-12 08:06:01 字数 90 浏览 0 评论 0原文

我想捕捉 iPhone 屏幕的特定部分。我使用了 UIGraphicsBeginImageContextWithOptions,但无法用它捕获屏幕的一部分。 请帮我。

I want to capture a particular portion of iPhone screen. I used UIGraphicsBeginImageContextWithOptions, but couldn't capture a portion of screen with this.
Please help me.

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

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

发布评论

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

评论(6

对你的占有欲 2024-12-19 08:06:01

您可以使用此代码

UIGraphicsBeginImageContext(self.view.bounds.size);

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

UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

CGRect 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 = CGRectMake(250,61 ,410, 255);
CGImageRef imageRef = CGImageCreateWithImageInRect([viewImage CGImage], rect);

UIImage *img = [UIImage imageWithCGImage:imageRef]; 

UIImageWriteToSavedPhotosAlbum(img, nil, nil, nil); 

CGImageRelease(imageRef);
节枝 2024-12-19 08:06:01

您可以使用UIGraphicsGetImageFromCurrentContext截取屏幕截图。下面的代码是凭记忆写的,所以可能会出现错误。请自行更正。

- (UIImage*)captureView:(UIView *)yourView {
    CGRect rect = [[UIScreen mainScreen] bounds];
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    [yourView.layer renderInContext:context];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

You can take screen shot using UIGraphicsGetImageFromCurrentContext. Wrote below code from memory, so errors possible. Please correct it yourself.

- (UIImage*)captureView:(UIView *)yourView {
    CGRect rect = [[UIScreen mainScreen] bounds];
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    [yourView.layer renderInContext:context];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}
白衬杉格子梦 2024-12-19 08:06:01

这是 UIView 的一个快速扩展,它将捕获整个视图或视图中的一个框架。它考虑了屏幕比例。

extension UIView {

    func imageSnapshot() -> UIImage {
        return self.imageSnapshotCroppedToFrame(nil)
    }

    func imageSnapshotCroppedToFrame(frame: CGRect?) -> UIImage {
        let scaleFactor = UIScreen.mainScreen().scale
        UIGraphicsBeginImageContextWithOptions(bounds.size, false, scaleFactor)
        self.drawViewHierarchyInRect(bounds, afterScreenUpdates: true)
        var image: UIImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        if let frame = frame {
            // UIImages are measured in points, but CGImages are measured in pixels
            let scaledRect = CGRectApplyAffineTransform(frame, CGAffineTransformMakeScale(scaleFactor, scaleFactor))

            if let imageRef = CGImageCreateWithImageInRect(image.CGImage, scaledRect) {
                image = UIImage(CGImage: imageRef)
            }
        }
        return image
    }
}

Here's a swift extension on UIView that will either capture the entire view or a frame within the view. It takes into account the screen scale.

extension UIView {

    func imageSnapshot() -> UIImage {
        return self.imageSnapshotCroppedToFrame(nil)
    }

    func imageSnapshotCroppedToFrame(frame: CGRect?) -> UIImage {
        let scaleFactor = UIScreen.mainScreen().scale
        UIGraphicsBeginImageContextWithOptions(bounds.size, false, scaleFactor)
        self.drawViewHierarchyInRect(bounds, afterScreenUpdates: true)
        var image: UIImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        if let frame = frame {
            // UIImages are measured in points, but CGImages are measured in pixels
            let scaledRect = CGRectApplyAffineTransform(frame, CGAffineTransformMakeScale(scaleFactor, scaleFactor))

            if let imageRef = CGImageCreateWithImageInRect(image.CGImage, scaledRect) {
                image = UIImage(CGImage: imageRef)
            }
        }
        return image
    }
}
(り薆情海 2024-12-19 08:06:01

@Superdev 的答案是正确的,我想添加的是一个小技巧,如果你想捕获父视图并避免子视图(特别是覆盖视图),你可以做的是使这个子视图成为一个属性并使用以下内容

CGFloat width = CGRectGetWidth(self.view.bounds);
CGFloat height = CGRectGetHeight(self.view.bounds);
_overlayView.hidden = YES;
UIGraphicsBeginImageContext(self.view.bounds.size);

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

UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

CGRect rect = CGRectMake(0,0 ,width, height);
CGImageRef imageRef = CGImageCreateWithImageInRect([viewImage CGImage], rect);

UIImage *img = [UIImage imageWithCGImage:imageRef];

UIImageWriteToSavedPhotosAlbum(img, nil, nil, nil);

CGImageRelease(imageRef);

_overlayView.hidden = NO;

这是一个小技巧,希望有人会发现它有用

the answer by @Superdev is right on spot , what i want to add is a little trick if you want to capture a parent view and avoid subviews ( in particular overlay view ) what you can do is make this subview a property and use the following

CGFloat width = CGRectGetWidth(self.view.bounds);
CGFloat height = CGRectGetHeight(self.view.bounds);
_overlayView.hidden = YES;
UIGraphicsBeginImageContext(self.view.bounds.size);

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

UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

CGRect rect = CGRectMake(0,0 ,width, height);
CGImageRef imageRef = CGImageCreateWithImageInRect([viewImage CGImage], rect);

UIImage *img = [UIImage imageWithCGImage:imageRef];

UIImageWriteToSavedPhotosAlbum(img, nil, nil, nil);

CGImageRelease(imageRef);

_overlayView.hidden = NO;

its a small trick , hope someone will find it useful

戒ㄋ 2024-12-19 08:06:01

另一种捕获清晰屏幕截图的方法;

if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)])
            UIGraphicsBeginImageContextWithOptions(self.captureView.frame.size, NO, [UIScreen mainScreen].scale);
        else
            UIGraphicsBeginImageContext(self.captureView.frame.size);

        [self.captureView.layer renderInContext:UIGraphicsGetCurrentContext()];
        UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
        [viewImage drawInRect:CGRectMake(0.0, 0.0, 640,960)];
        //NSData*m_Imgdata=UIImagePNGRepresentation(viewImage);
        NSData*m_Imgdata=UIImageJPEGRepresentation(viewImage, 1.0);

       // UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);

        UIGraphicsEndImageContext();

Another method to capture clear screenshot;

if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)])
            UIGraphicsBeginImageContextWithOptions(self.captureView.frame.size, NO, [UIScreen mainScreen].scale);
        else
            UIGraphicsBeginImageContext(self.captureView.frame.size);

        [self.captureView.layer renderInContext:UIGraphicsGetCurrentContext()];
        UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
        [viewImage drawInRect:CGRectMake(0.0, 0.0, 640,960)];
        //NSData*m_Imgdata=UIImagePNGRepresentation(viewImage);
        NSData*m_Imgdata=UIImageJPEGRepresentation(viewImage, 1.0);

       // UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);

        UIGraphicsEndImageContext();
她说她爱他 2024-12-19 08:06:01

SWIFT 3.0 Xcode8版本
来自@jDutton 的回答并为我工作。

extension UIView {
    func imageSnapshot() -> UIImage {
        return self.imageSnapshotCroppedToFrame(frame: nil)
    }

    func imageSnapshotCroppedToFrame(frame: CGRect?) -> UIImage {
        let scaleFactor = UIScreen.main.scale
        UIGraphicsBeginImageContextWithOptions(bounds.size, false, scaleFactor)
        self.drawHierarchy(in: bounds, afterScreenUpdates: true)
        var image: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
        UIGraphicsEndImageContext()

        if let frame = frame {
            // UIImages are measured in points, but CGImages are measured in pixels
            let scaledRect = frame.applying(CGAffineTransform(scaleX: scaleFactor, y: scaleFactor))

            if let imageRef = image.cgImage!.cropping(to: scaledRect) {
                image = UIImage(cgImage: imageRef)
            }
        }
        return image
    }
}

如果您崩溃并收到以下消息:

GContextSaveGState: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
CGContextRestoreGState: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.

请尝试此解决方案

希望能节省您的时间!

SWIFT 3.0 Xcode8 Version
come from @jDutton answer and work for me.

extension UIView {
    func imageSnapshot() -> UIImage {
        return self.imageSnapshotCroppedToFrame(frame: nil)
    }

    func imageSnapshotCroppedToFrame(frame: CGRect?) -> UIImage {
        let scaleFactor = UIScreen.main.scale
        UIGraphicsBeginImageContextWithOptions(bounds.size, false, scaleFactor)
        self.drawHierarchy(in: bounds, afterScreenUpdates: true)
        var image: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
        UIGraphicsEndImageContext()

        if let frame = frame {
            // UIImages are measured in points, but CGImages are measured in pixels
            let scaledRect = frame.applying(CGAffineTransform(scaleX: scaleFactor, y: scaleFactor))

            if let imageRef = image.cgImage!.cropping(to: scaledRect) {
                image = UIImage(cgImage: imageRef)
            }
        }
        return image
    }
}

if you crash and get the following message:

GContextSaveGState: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
CGContextRestoreGState: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.

try this solution

Hope for saving your time!

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