如何将NSView的内容保存到比它更大的文件中

时间:2012-04-27 06:13:25

标签: macos cocoa nsview

我有NSView 300x300px,但我需要将我的NSView的内容图像保存到文件中1000x1000px。可能吗?我不想改变NSView的大小,只是为了保存。

到目前为止,我有这个:

NSImage *viewImage = [[NSImage alloc] initWithData:[self dataWithPDFInsideRect:[self bounds]]];

[[viewImage TIFFRepresentation] writeToFile:@"..." atomically:YES];

它可以很好地保存300x300px图像(视图的大小),但是如何使用View的内容与300x300更大的1000x1000px输出相同来制作1000x1000px?

2 个答案:

答案 0 :(得分:0)

Create a new image of the desired sizelock focus on itscale by(所需大小÷视图大小)(考虑宽高比,除非您不介意拉伸内容),{{ 3}}图片上tell the view to drawthe view's bounds中的所有内容,并保存图片。

答案 1 :(得分:0)

你可以试试,

假设您想将NSImage保存到文件,

// it will save in the user document folder 
+(NSString *)writeImageToFile:(NSImage *)pImage FileName:(NSString *)pFileName{

    // Make the Complete File name 
    NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

    // yes it should be in the user document folder by default 
    NSString *pCompleteFileName = [docDir stringByAppendingPathComponent:pFileName];

    // now we can save the same 
    [[pImage TIFFRepresentation] writeToFile:pCompleteFileName atomically:YES];

    return pCompleteFileName;


}