NSView 转换成NSImage,并保存

来源:互联网 发布:sql server能卸载吗 编辑:程序博客网 时间:2024/06/07 09:18

    [zwViewlockFocus];//zwView为继承NSView类的一个对象

    NSImage *image = [[NSImagealloc] initWithData:[zwViewdataWithPDFInsideRect:[zwViewbounds]]];

    [zwViewunlockFocus];

    [imagelockFocus];

    //先设置下面一个实例

    NSBitmapImageRep *bits = [[NSBitmapImageRepalloc]initWithFocusedViewRect:[zwViewframe]];

    [imageunlockFocus];

    //再设置后面要用到得 props属性

    NSDictionary *imageProps = [NSDictionarydictionaryWithObject:[NSNumbernumberWithBool:0]forKey:NSImageCompressionFactor];

    

    //之后转化为NSData 以便存到文件中

    

    

    NSData *imageData = [bitsrepresentationUsingType:NSPNGFileTypeproperties:imageProps];

    //设定好文件路径后进行存储就ok

    [imageData writeToFile:[[[NSStringalloc] initWithFormat:@"~/Documents/test%d.jpg",1]stringByExpandingTildeInPath]atomically:YES];   //保存的文件路径一定要是绝对路径,相对路径不行


0 0