NSView 转换成NSImage,并保存

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

    NSImage *image = [[NSImage alloc] initWithData:[zwView dataWithPDFInsideRect:[zwView bounds]]];

    [zwView unlockFocus];

    [image lockFocus];

    //先设置 下面一个实例

    NSBitmapImageRep *bits = [[NSBitmapImageRep alloc]initWithFocusedViewRect:[zwView frame]];

    [image unlockFocus];

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

    NSDictionary *imageProps = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:0] forKey:NSImageCompressionFactor];

    

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

    

    

    NSData *imageData = [bits representationUsingType:NSPNGFileType properties:imageProps];

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

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


猜你喜欢

转载自blog.csdn.net/qq_27740983/article/details/50370018