This is a very quick one. For an app I’m working on I wanted to measure how long it took a given function to complete. I typically don’t do this, as most functions are very fast, but this one involved a server fetch and some background processing. Anyhow, I’m getting distracted, here it is -easy …
Author Archives: mmv
Dealing with UIGraphics ImageContext ‘Memory Leaks’
Here’s the code to change the image size of an image in iOS so that then it can be saved with smaller footprint: let rect = CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height) // Actually do the resizing to the rect using the ImageContext stuff UIGraphicsBeginImageContextWithOptions(newSize, false, 1.0) image.draw(in: rect) …
Continue reading “Dealing with UIGraphics ImageContext ‘Memory Leaks’”
Pre-Scrolling a Table to a specific cell at load
Although that’s something that appears fairly easy, the truth -for me at least-, is that scrolling a UITableView to a specific cell every time you load it may prove tricky. In the app I’m working on I implement it in the following way: every time a view appeared I assess if the top visible row …
Continue reading “Pre-Scrolling a Table to a specific cell at load”
Find the right value in an Array, or in an Array of Dictionaries
From time to time I discover a new way of coding something that significantly improves readability & minimizes how much I need to code. Here’s one of such latest discoveries. Typically to find a value in an array (or in an array of dictionaries) with a specific condition I would do a for loop with …
Continue reading “Find the right value in an Array, or in an Array of Dictionaries”
Inserting cells at the top of a UITableView with no scrolling
If you would like your table to be refreshed “ala Twitter”, with new data being added on top, here’s a quick an easy way to do it for iOS: func updateTableViewAfterAdditionInNewArray(){ //Get new feeds and check if counts are different let countOfAddedItems = oldArray.count – newArray.count …
Continue reading “Inserting cells at the top of a UITableView with no scrolling”
Improve your ⌘ V efficiency
Want to improve your quality of life in 5 seconds? 1. Go to System Preferences 2. Keyboard 3. Shortcuts -> App Shortcuts 4. Click on add -> All Applications –> add “Paste and Match Style” for keyboard shortcut ⌘V Thank you Ally Marc
Multi Thread access to shared Variables – Swift 4 Concurrency
Working with background threads really helps improve the app responsiveness & the overall user experience. But, and it’s a big but, you have to be aware of multithread access conflicts. If you access/modify the same variable from multiple threads you’re prone to non-reproducible -seemingly random- crashes. How to Detect Conflicts? To help you find where …
Continue reading “Multi Thread access to shared Variables – Swift 4 Concurrency”
Random numbers in Swift
Swift 4.2 has introduced a native random number API in the standard library, making it cross platform. Int.random(in: 1…1000) // → 691 Double.random(in: 0..<1) // → 0.8741555749903935 UInt32.random(in: 0xD800…0xDFFF) // → 55666 Read more in the great article from Ole Begemann, worth a read. Marc
Resize NSImage Proportionally (Swift)
I wanted a simple function to resize an image to a desired height, while keeping proportions. Here’s what I came up with (based on Marco implementation): extension NSImage { func resizedImageTo(sourceImage: NSImage, newSize: NSSize) -> NSImage?{ if sourceImage.isValid == false { …
Change Background Color of your app Title Bar
Subclass your NSWindow Controller, then in Window Did Load add the following two lines: //Signature Color window?.titlebarAppearsTransparent = true window?.backgroundColor = NSColor.blue That’s it, here’s the result you’ll get: Comments / questions? I’m at @MarcMasVi Marc