Tag: 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!

18
Jul

XCode 7 Beta 3 crash after opening project

Apple recently released another beta version of it’s XCode 7. Unfortunately it comes not only with fixes but with one extremely annoying bug. After you try to create new or open existing project it just crashes. Lucky for us there is a quick solution for this. We need to remove some XCode configuration file (which will be regenerated after XCode will launch).

rm ~/Library/Preferences/com.apple.dt.Xcode.plist

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..