Posts

Post not yet marked as solved
2 Replies
400 Views
Hello, this is the code I'm using to save a screenshot of the current metal view. After saving 180 images, the app crashes with a memory issue. autoreleasepool{                     let   fileName = captureFileName + String(currentIdx) + ".png"                     if let imageRef = view.currentDrawable!.texture.toImage() {                         let uiImage:UIImage = UIImage.init(cgImage: imageRef)                         if let data = uiImage.jpegData(compressionQuality: 1.0) {                             let filename = getDocumentsDirectory().appendingPathComponent(fileName)                             try? data.write(to: filename)                         }                     } } extension MTLTexture {     func bytes() -> UnsafeMutableRawPointer {         let width = self.width         let height   = self.height         let rowBytes = self.width * 4         let p = malloc(width * height * 4)         self.getBytes(p!, bytesPerRow: rowBytes, from: MTLRegionMake2D(0, 0, width, height), mipmapLevel: 0)         return p!     }     func toImage() -> CGImage? {                  let p = bytes()         let pColorSpace = CGColorSpaceCreateDeviceRGB()         let rawBitmapInfo = CGImageAlphaInfo.noneSkipFirst.rawValue | CGBitmapInfo.byteOrder32Little.rawValue         let bitmapInfo:CGBitmapInfo = CGBitmapInfo(rawValue: rawBitmapInfo)         let selftureSize = self.width * self.height * 4         let rowBytes = self.width * 4         let releaseMaskImagePixelData: CGDataProviderReleaseDataCallback = { (info: UnsafeMutableRawPointer?, data: UnsafeRawPointer, size: Int) -> () in                          return         }         let provider = CGDataProvider(dataInfo: nil, data: p, size: selftureSize, releaseData: releaseMaskImagePixelData)         let cgImageRef = CGImage(width: self.width, height: self.height, bitsPerComponent: 8, bitsPerPixel: 32, bytesPerRow: rowBytes, space: pColorSpace, bitmapInfo: bitmapInfo, provider: provider!, decode: nil, shouldInterpolate: true, intent: CGColorRenderingIntent.defaultIntent)!                           return cgImageRef     } } I figured out that the malloced p in the extension function causes the memory issue but how do I free this after using the pointed data inside autoreleasepool? Thanks!
Posted
by kjlee818.
Last updated
.
Post not yet marked as solved
0 Replies
290 Views
For drawing points in metal I could only determined the point size. Will it be possible to draw surfels (XYZ, RGB, normals, radius)?Thank you so much!
Posted
by kjlee818.
Last updated
.