NSView转换为NSimage 与 UIImage转换为UIimage 总结
2016-07-21 12:44
369 查看
Cocoa框架下NSView转换为NSimage 与 UIKit框架下UIView转化为UIImage对比总结
Cocoa框架下:
UIKit框架下:
Cocoa框架下:
/*! * @author GJH, 16-07-21 * * NSview 转换为 NSimage * * @param m_view 需要转换的NSView对象 * * @return 所要生成的NSimage */ -(NSImage *)viewToImage:(NSView *)m_view { // 焦点锁定 [m_view lockFocus]; // 生成所需图片 NSImage *image = [[NSImage alloc]initWithData:[m_view dataWithPDFInsideRect:[m_view bounds]]]; [m_view unlockFocus]; // 保存图片到本地 [image lockFocus]; NSBitmapImageRep *bits = [[NSBitmapImageRep alloc]initWithFocusedViewRect:[m_view bounds]]; [image unlockFocus]; // 设置要用到的props属性 NSDictionary *imageProps = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO] forKey:NSImageCompressionFactor]; // 转化为Data保存 NSData *imageData = [bits representationUsingType:NSPNGFileType properties:imageProps]; // 保存路径必须是绝对路径相对路径不行 [imageData writeToFile:[[NSString alloc]initWithFormat:@"/Users/gjh/Desktop/ImageData/test%d.png",1] atomically:YES]; return image; }
UIKit框架下:
/*! * @author GJH, 16-07-21 * * UIView转化为UIImage * * @param mView 需要转化的View * * @return 生成的UIimage */ - (UIImage *)callBackImageWithView:(UIView *)mView{ // 创建画板与视图一致 UIGraphicsBeginImageContextWithOptions(mView.bounds.size, YES, [UIScreen mainScreen].scale); // 将视图画在画板上 [mView drawViewHierarchyInRect:mView.bounds afterScreenUpdates:YES]; // 将画板转化为UIimage UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return image; }
相关文章推荐
- WebRequestHelper
- SUI的无限下拉和分页效果的实现
- NSProcessInfo获取进程信息,UIDevice获取设信息
- Wiggle Subsequence
- PHP Failed opening required (include_path='.;C:\php\pear')
- 如何在android应用里执行adb 命令深入研究java.lang.ProcessBuilder类
- Java之String、StringBuffer、StringBuilder
- Android Bluetooth(蓝牙)实例
- refind实现Win10 + Kali2.0 双引导
- mac OS X10.11.5安装scikit-learn后运行出现ValueError: numpy.dtype has the wrong size的错误
- UE4里启动物件的碰撞机制
- iOS 7.0 使用定位requestAlwaysAuthorization导致crash的问题
- poj Subsequence
- pat 1099. Build A Binary Search Tree (30)
- 关于js无法设置input的value的问题
- UILabel简单使用
- 12C - Fruits
- 22个android源码--UI
- vue 列表渲染
- V-Behavioral Questions