Category: iOS

13
Sep

MKMapView too slow? Use preview – MKMapSnapshotter

Did you ever needed to just show a piece of map and added it disabling all touch events and end up with crazy slow unneccesary MKMapView? I’ve recently discovered

MKMapSnapshotter

which solves all the issues. We can use it since iOS7 so MKMapView can be easily replaced and snapshotter result (which is an image) can be cached – clean and quick!

import MapKit
import CoreLocation

let coords = CLLocationCoordinate2D(latitude: 52.235799, longitude: 20.995958)
let distanceInMeters: Double = 500
let size = CGSize(width: 200, height: 200)

let options = MKMapSnapshotOptions()
options.region = MKCoordinateRegionMakeWithDistance(coords, distanceInMeters, distanceInMeters)
options.size = size

let snapShotter = MKMapSnapshotter(options: options)
let bgQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
// this might be run in foregourd but we want to have responsive UI
// which helped a lot being a UITableViewCell which lagged a lot
snapShotter.startWithQueue(bgQueue) { [weak self] (snapShot, error) in
    guard error == nil else {
        return
    }

    if let snapShotImage = snapShot?.image, let coordinatePoint = snapShot?.pointForCoordinate(coords), let pinImage = UIImage(named: "pinImage") {
        UIGraphicsBeginImageContextWithOptions(snapShotImage.size, true, snapShotImage.scale)
        snapShotImage.drawAtPoint(CGPointZero)
        // need to fix the point position to match the anchor point of pin which is in middle bottom of the frame
        let fixedPinPoint = CGPointMake(coordinatePoint.x - pinImage.size.width / 2, coordinatePoint.y - pinImage.size.height)
        pinImage.drawAtPoint(fixedPinPoint)
        let mapImage = UIGraphicsGetImageFromCurrentImageContext()
        dispatch_async(dispatch_get_main_queue(), {
            self?.mapPreviewImageView.image = mapImage
            // stop and hide spinner which was in image center place
            self?.activityIndicator.stopAnimating()
        })
        UIGraphicsEndImageContext()
    }
}

I’ve put some comments to explain some non standard stuff but overall it should be simple enough. If you have any questions just ask!

05
Jun

Swift first steps: Singleton

Next day next challenge, as simple as it may be – creating Singleton – it can bring some problems in now known language using not known syntax. Lucky enough I was able to write my first Swift class and make it a working singleton (I have wrote unit tests that will testify it). So let’s take a look and next explain.

import Foundation

class Singleton {
    static let sharedInstance = Singleton()
    private init() {}
}

Continue Reading..

03
Jun

Apple’s Swift for iOS

Swift was introduced on WWDC by Apple as a new programming language for iOS and OS X. Apple claims that it’s much faster than Objective-C and has better syntax but let’s get deeper into to to judge ourselves. Official apple note:

Swift is an innovative new programming language for Cocoa and Cocoa Touch. Writing code is interactive and fun, the syntax is concise yet expressive, and apps run lightning-fast. Swift is ready for your next iOS and OS X project — or for addition into your current app — because Swift code works side-by-side with Objective-C.

Now we all have to download the Apple’s book and dive into it. Below I present some of my first impressions.Continue Reading..

27
Nov

ScrollView has ambiguous scrollable content height

Autolayout without coding - ScrollView

Recently while doing application upgrade to iOS7 I’ve encountered few issues with autolayout machanism and I want to share with you one in particular. I hope that will help you make your app ‘responsive’ 🙂

Let’s go back in history, we had an app called iDoc24 and after iOS7 was greatly adopted I had to start putting all things aside and prepare update with new style. Here  how the main menu looked back then and what was the result of upgrading.

synappse idoc24 first derm main menu 1
synappse idoc24 first derm main menu 2
Continue Reading..