截取UIImage iOS的截图 [英] Take Screenshot of a UIImage iOS

查看:473
本文介绍了截取UIImage iOS的截图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在iOS应用中截取屏幕的特定部分。附加的图像是屏幕的一部分。在这里,我想截取包含3个UIImageViews的红色标记矩形区域的屏幕截图,其中包含背景处的白色图像帧,带有咖啡杯的图像和咖啡上的苹果标志图像。

I want to take screenshot of a perticular section of my screen in a iOS App. The attached image is that part of screen. Here I want to take screenshot of the red marked rectangular area containing 3 UIImageViews which contains a white Image Frame at background , an image with coffee cup and the apple sign image on the coffee respectively.

I我正在使用以下代码执行该操作...

I am using the following code for doing that...

- (UIImage *)captureView:(UIView *)view withArea:(CGRect)screenRect {

    UIGraphicsBeginImageContext(screenRect.size);

    CGContextRef ctx = UIGraphicsGetCurrentContext();
    [[UIColor blackColor] set];
    CGContextFillRect(ctx, screenRect);

    [view.layer renderInContext:ctx];

    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return newImage ;
}

这样称呼

UIImage *viewImage = [self captureView:self.FrameImageView withArea:self.FrameImageView.bounds];

此处FrameImageView是一个UIImageView,它在背景中包含白色图像帧。

Here FrameImageView is an UIImageView that contains the white Image frame at the background.

但我得到的结果如下图所示。

But I am getting the resulting image like the following.

我认为问题是,我的代码正在截取FrameImageView层。这就是为什么我只获得背景框架。但是我希望屏幕截图在FrameImageView上包含其他2个UIImageViews,就像第一个图像的红色矩形部分一样。

I think the problem is , my code is taking screenshot of the layer of FrameImageView. That's why I am getting the background frame only. But I want the screenshot to contain the other 2 UIImageViews over FrameImageView as well like the first image's red rectangle section.

任何人都可以帮助我,如何解决这个问题。在此先感谢。

can anyone help me , how to fix that. Thanks in Advance.

推荐答案

试试这个:

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

这篇关于截取UIImage iOS的截图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆