保存整个NSView内容,这是NSScrollView内部 [英] Saving entire NSView contents, which is inside an NSScrollView

查看:275
本文介绍了保存整个NSView内容,这是NSScrollView内部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试将NSView的内容保存为图片,但只保存scrollview中可见的图片。

I'm trying to save contents of an NSView as an image, but, only the image visible inside the scrollview is saved.

这里是实际加载的图片在视图中:

Here is the actual image loaded in the view: .

但这是我能够保存的形象:

But this is how I'm able to save the image:

有没有办法保存整个图像在NSView?

Is there any way to save the entire image in the NSView?

这是我如何保存我的NSView子类:

This is how I'm saving my NSView subclass:

- (void)save
{
    [self lockFocus];
    NSBitmapImageRep* rep = [[NSBitmapImageRep alloc] initWithFocusedViewRect:[self bounds]];  
    [self unlockFocus];

    NSData* data = [rep representationUsingType:NSPNGFileType properties:nil];

    NSString* string = NSHomeDirectory();
    NSString* pth1 = [string stringByAppendingPathComponent:@"ttx.png"];

    [data writeToFile:pth1 atomically:YES];
}

此方法是在我要保存的视图中。

This method is in the View i want to save.

推荐答案

使用 - [NSView cacheDisplayInRect:toBitmapImageRep:]

NSBitmapImageRep* rep = [self bitmapImageRepForCachingDisplayInRect:self.bounds];
[self cacheDisplayInRect:self.bounds toBitmapImageRep:rep];

这篇关于保存整个NSView内容,这是NSScrollView内部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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