【问题标题】:Howe to capture UIView top UIView如何捕获 UIView 顶部 UIView
【发布时间】:2012-12-12 13:19:27
【问题描述】:

我有UIView,它显示了图表。现在我需要将它捕获到UIImage,然后我用谷歌搜索它并得到下面的代码。但如果我在我的代码中使用它,它就不起作用。即使我使用断点编译器无法达到这个。我在我的UIView 中使用它。我哪里出错了?

+ (UIImage *) imageWithView:(UIView *)view
{
UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];

UIImage * img = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return img;
} 

【问题讨论】:

  • 你是怎么调用这个方法的..?
  • 你现在在哪里打电话?

标签: iphone ios uiview uiimage


【解决方案1】:

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

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

}

然后像下面这样称呼它......

UIImage *tempImageSave=[self captureView];

你也可以用下面这条线保存这张图片作为相册..

UIImageWriteToSavedPhotosAlbum(tempImageSave,nil,nil,nil);

您还可以使用下面的文档目录行保存此图像..

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

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

-(UIImage *)convertViewToImage:(UIView *)viewTemp
{
    UIGraphicsBeginImageContext(viewTemp.bounds.size);
    [viewTemp drawViewHierarchyInRect:viewTemp.bounds afterScreenUpdates:YES];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return image;

}

然后像下面这样使用这个方法。

UIImage *tempImageSave = [self convertViewToImage:yourView];

我希望这对你有帮助。

【讨论】:

  • 需要在 UIView 中使用这段代码吧??但未找到属性视图错误
  • 嘿伙计,看看你想捕捉哪个视图??你想要这个图像是什么?你想将此图像保存在设备目录中吗??
  • @Christien 只是将上面的方法放在你的 .m 文件中,然后像我上面的代码一样调用它,还将这个捕获的图像添加到 UIImageView 中,并检查它显示的正确照片,你捕获的花花公子.. :)
  • @Christien dude 我的最后一行将您捕获的视图图像保存在您的目录中,非常简单
  • @ParasJoshi 将其保存到设备相册中,而不是在 Xcode 模拟器文档目录中:)
【解决方案2】:

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

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

【讨论】:

    【解决方案3】:

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

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

    【讨论】:

    • 需要在 UIView 中使用这段代码吧??但未找到属性视图错误
    • 你把你在“self.view.bounds.size”中定义的视图放到你的VIew.bounds.size
    • 对不起我忘了放一行 [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
    • 检查我更新的答案下载演示并检查它的图像存储在文档目录中:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-25
    • 1970-01-01
    相关资源
    最近更新 更多