Post

Replies

Boosts

Views

Activity

Reply to App crashes (UIGraphicsImageRenderer, PDF to images with PDFKit)
Seeing you're producing any UI view or controllers meant for the main thread, you can run code in a background thread and just pass ima back to the main thread or write it a local disk. import UIKit import PDFKit func extractImage(completion: @escaping ((UIImage)->Void)) {     guard let path = Bundle.main.path(forResource: "filename", ofType: "pdf") else { return }     let url = URL(fileURLWithPath: path)     // Instantiate a `CGPDFDocument` from the PDF file's URL.     guard let document = PDFDocument(url: url) else { return }     // Get the first page of the PDF document.     guard let page = document.page(at: 0) else { return }     // Fetch the page rect for the page we want to render.     let pageRect = page.bounds(for: .mediaBox)     let renderer = UIGraphicsImageRenderer(size: pageRect.size)     let img = renderer.image { ctx in         // Set and fill the background color.         UIColor.white.set()         ctx.fill(CGRect(x: 0, y: 0, width: pageRect.width, height: pageRect.height))         // Translate the context so that we only draw the `cropRect`.         ctx.cgContext.translateBy(x: -pageRect.origin.x, y: pageRect.size.height - pageRect.origin.y)         // Flip the context vertically because the Core Graphics coordinate system starts from the bottom.         ctx.cgContext.scaleBy(x: 1.0, y: -1.0)         // Draw the PDF page.         page.draw(with: .mediaBox, to: ctx.cgContext)     }          DispatchQueue.main.async {         completion(img)     } } DispatchQueue.global().async {     // call extract on a global Q     extractImage { image in         // Return UIImage on main thread              } }
Nov ’21
Reply to Reference to currently active window
import UIKit extension UIApplication {     var topViewController: UIViewController? {         var topViewController: UIViewController? = nil         if #available(iOS 13, *) {             topViewController = connectedScenes.compactMap {                 return ($0 as? UIWindowScene)?.windows.filter { $0.isKeyWindow  }.first?.rootViewController             }.first         } else {             topViewController = keyWindow?.rootViewController         }         if let presented = topViewController?.presentedViewController {             topViewController = presented         } else if let navController = topViewController as? UINavigationController {             topViewController = navController.topViewController         } else if let tabBarController = topViewController as? UITabBarController {             topViewController = tabBarController.selectedViewController         }         return topViewController     } } // handle any checks for any custom contollers outside of extension if let controller = UIApplication.shared.topViewController as? CPTViewController { } There is no need for the loop, waste of cpu cycles.
Nov ’21
Reply to M1 MacBook Pro Trackpad not clicking
This seems to be a problem with Monterey. I have noticed at times the click area sometimes is unresponsive, the cursor will move halfway across the screen or the UI doesn't respond to the first click. Wait and see what happens when 15.1 is release or you might have a swollen battery causing the track pad issue.
Nov ’21