首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何捕获UIView顶级UIView

如何捕获UIView顶级UIView
EN

Stack Overflow用户
提问于 2012-12-27 13:31:51
回答 3查看 786关注 0票数 2

我有UIView,它显示了我需要把它捕获到UIImage中的Graph..now,我用谷歌搜索了它,得到了下面的code.but如果我在代码中使用了它,它不是work.even如果我使用断点编译器不能到达它我在我的UIView .where中使用它我会出错吗?

代码语言:javascript
复制
+ (UIImage *) imageWithView:(UIView *)view
{
UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];

UIImage * img = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return img;
} 
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-12-27 13:34:28

您可以使用以下方法截取iPhone应用程序的任何视图或整个屏幕的屏幕截图

代码语言:javascript
复制
- (UIImage *)captureView {

//hide controls if needed
CGRect rect = [self.view bounds];

    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    [self.view.layer renderInContext:context];   
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return img;

}

就叫它贝罗吧..。

代码语言:javascript
复制
UIImage *tempImageSave=[self captureView];

你也可以用下面这行来保存这张图片作为相册。

代码语言:javascript
复制
UIImageWriteToSavedPhotosAlbum(tempImageSave,nil,nil,nil);

您还可以使用以下行保存此图像作为Document Directory。

代码语言:javascript
复制
NSData *imageData = UIImagePNGRepresentation(tempImageSave);
NSFileManager *fileMan = [NSFileManager defaultManager];

NSString *fileName = [NSString stringWithFormat:@"%d.png",1];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *pdfFileName = [documentsDirectory stringByAppendingPathComponent:fileName];
[fileMan createFileAtPath:pdfFileName contents:imageData attributes:nil];

如果视图包含层图像或一些与图形相关的数据,则使用以下方法。

代码语言:javascript
复制
-(UIImage *)convertViewToImage:(UIView *)viewTemp
{
    UIGraphicsBeginImageContext(viewTemp.bounds.size);
    [viewTemp drawViewHierarchyInRect:viewTemp.bounds afterScreenUpdates:YES];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return image;

}

并使用下面的方法。

代码语言:javascript
复制
UIImage *tempImageSave = [self convertViewToImage:yourView];

我希望这对你有帮助。

票数 3
EN

Stack Overflow用户

发布于 2012-12-27 13:37:42

您可以像这样保存视图图像,并将图像存储到文档目录中:-

代码语言:javascript
复制
-(void)SaveViewAsImage
{
    UIGraphicsBeginImageContext(self.view.bounds.size);

        UIImage *saveImage = UIGraphicsGetImageFromCurrentImageContext();
        [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
        UIGraphicsEndImageContext();
        NSData *imageData = UIImagePNGRepresentation(saveImage);
        NSFileManager *fileMan = [NSFileManager defaultManager];

        NSString *fileName = [NSString stringWithFormat:@"%d.png",1];
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *pdfFileName = [documentsDirectory stringByAppendingPathComponent:fileName];
        [fileMan createFileAtPath:pdfFileName contents:imageData attributes:nil];

}

您的图像保存在文档目录中:)

下载此演示

http://www.sendspace.com/file/gjxa82

票数 1
EN

Stack Overflow用户

发布于 2012-12-27 13:56:06

尝试使用此代码,它可能会对您有所帮助

代码语言:javascript
复制
- (UIImage *)captureView:(UIView *)view {
    CGRect screenRect = [[UIScreen mainScreen] bounds];

    UIGraphicsBeginImageContext(screenRect.size);

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

    [view.layer renderInContext:ctx];

    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return newImage;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14049796

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档