Skip to main content

Posts

Showing posts from 2023

How to Cache NSDateFormatter?

Instantiating NSDateFormatter objects is slow. Most well-written apps that use NSDateFormatter will make efforts to reduce the number unique instances created. Using a static property is sufficient for most cases, but what if you work on a large app where lots of NSDateFormatter objects are needed by a large number of classes. Wouldn’t it be nice if there was an NSCachedDateFormatter that keeps track of what it has instantiated and will return cached objects and only instantiate new ones when necessary? Wouldn’t it be nice if NSCachedDateFormatter was a singleton and could be used app-wide? Here's a sample class that can be used to fulfill this need.

Zooming Images with Text in an iOS Scroll View

You have an image containing text. As such, there’s lots of sharp edges. You want to put this image into a scroll view and allow the user to zoom in with a pinch gesture. This is very easy to implement in iOS. Technically, you don’t even need to write code. This can all be done in Interface Builder. However, the further the image is zoomed the blurrier it looks. Solution 1 Start with a fully zoomed of the image and scale it down when initially adding it to the scroll view. After all, it’s much easier to scale an image down and keep it looking good than it is to scale it up. So during the whole zoom operation the image being displayed is a scaled down version of the original. This would work great except that iOS doesn’t apply the right interpolation when scaling the original large down to fit into the initial smaller frame. You would think self.imageView.layer.minificationFilter = kCAFilterTrilinear; would do the trick but it doesn’t. I could be doi