目前我正在製作一個等角地圖編輯器(在斯威夫特),其中每個地圖塊添加到SKNode
稱爲mapLayer
。我想知道一旦完成地圖的設計,是否可以從這個mapLayer
中生成一個png圖像?從Xcode的SpriteKit Node/SpriteNode創建一個PNG圖像爲Mac
-1
A
回答
0
不是在Swift中,但它應該給你一個關於如何去做的好主意。
你可以這樣做捕獲屏幕到一個UIImage:
CGRect bounds = self.scene.view.bounds;
UIGraphicsBeginImageContextWithOptions(bounds.size, NO, [UIScreen mainScreen].scale);
[self drawViewHierarchyInRect:bounds afterScreenUpdates:YES];
UIImage* screenshotImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
然後創建一個從UIImage的PNG圖像(並將其寫入到磁盤)是這樣的:
// Create paths to output images
NSString *pngPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Test.png"];
NSString *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Test.jpg"];
來源here 。
// Write a UIImage to JPEG with minimum compression (best quality)
// The value 'image' must be a UIImage object
// The value '1.0' represents image compression quality as value from 0.0 to 1.0
[UIImageJPEGRepresentation(image, 1.0) writeToFile:jpgPath atomically:YES];
// Write image to PNG
[UIImagePNGRepresentation(image) writeToFile:pngPath atomically:YES];
// Let's check to see if files were successfully written...
// Create file manager
NSError *error;
NSFileManager *fileMgr = [NSFileManager defaultManager];
// Point to Document directory
NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
// Write out the contents of home directory to console
NSLog(@"Documents directory: %@", [fileMgr contentsOfDirectoryAtPath:documentsDirectory error:&error]);
來源here。
0
下面是如何將視圖內容寫入PNG文件的示例。首先,定義一個UIView擴展,捕獲視圖的內容並將其轉換爲UIImage
。
extension UIView {
func screenGrab() -> UIImage {
// Uncomment this (and comment out the next statement) for retina screen capture
// UIGraphicsBeginImageContextWithOptions(bounds.size, false, UIScreen.mainScreen().scale)
UIGraphicsBeginImageContextWithOptions(bounds.size, false, 1.0)
drawViewHierarchyInRect(self.bounds, afterScreenUpdates: true)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
}
然後,定義一個將UIImage
寫入文件的UIImage擴展。
extension UIImage {
func writeToFile(fileName:String) {
let path = NSHomeDirectory().stringByAppendingPathComponent("Documents/"+fileName)
UIImagePNGRepresentation(self).writeToFile(path, atomically: true)
}
}
最後,採集和
self.view?.screenGrab().writeToFile("capture.png")
寫視圖爲PNG文件的內容,這裏是如何確定的PNG圖像將被存儲:
println(NSHomeDirectory().stringByAppendingPathComponent("Documents"))
相關問題
- 1. 從png創建圖像,轉換爲base64
- 2. 動態創建一個PNG圖像iPhone
- 3. PHP:如何從另一個PNG圖像創建圖像
- 4. 創建一個動態PNG圖像
- 5. 的XCode Spritekit,碰撞與PNG
- 6. Mac OS。如何從PNG數據創建圖像?
- 7. opencv的 - 創建PNG圖像
- 8. 創建一個與現有PNG圖像具有相同XMODEM-CRC的PNG圖像
- 9. 從QRCode創建png圖像iPhone版
- 10. 使用php從URL創建png圖像
- 11. Java - 從PNG圖像創建形狀(NullPointerException)
- 12. 使用鳴從PNG圖像創建一個FLV視頻
- 13. php GD爲ean13條形碼創建一個透明PNG圖像
- 14. PHP - 從圖像創建一個圖像
- 15. 創建一個從像素的圖像
- 16. 使用.png中的圖像創建.png圖像
- 17. PHP的GD創建一個透明的PNG圖像
- 18. (iphone)我從圖像創建一個新的圖像時,創建一個泄漏?
- 19. Xcode png圖像參考
- 20. 如何創建只有一個字節的PNG圖像
- 21. 從PNG圖像的不透明像素創建區域
- 22. 創建的NodeJS PNG圖像裏面
- 23. 在D中創建新的.png圖像
- 24. 從Xcode創建一個執行文件mac project
- 25. 從.jpeg/png創建一個動畫.gif
- 26. 在Xcode中創建一個暫停菜單SpriteKit
- 27. 如何從一個base64 PNG圖像
- 28. 從管道讀取一個PNG圖像
- 29. 如何在vb.net中創建一個空白PNG圖像
- 30. 如何創建反向PNG圖像?
我把它這將適用於iOS,因爲它使用UIKit,但我希望它適用於Mac應用程序。有使用AppKit的等價物嗎? – wes
對不起。我錯過了osx標籤。我必須研究這一點。 – Epsilon