2017年10月11日 星期三

iOS簡繁互轉

UIAlertController客製化

根據🍎的文件
The UIAlertController class is intended to be used as-is and does not support subclassing. The view hierarchy for this class is private and must not be modified.

如果不想理他的話,就用以下的方法吧!

  1. 打開contentViewController的束縛
    @interface UIAlertController (ContentViewController)
    @property (nonatomic,retain) UIViewController * contentViewController;
    @end
  2. 呼喚神秘的contentViewController
    alertController.contentViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "contentViewController")
  3. 如果,使用tableview的話,記得加上
    override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()
            
        // make UITableView fit to content's size
        self.preferredContentSize = self.tableView.contentSize

    }

code

2017年1月10日 星期二

iOS launch image的黑魔法

  1. 開一個新專案,在iPhone 7 size是 414x736

  2. 移除LaunchScreen.storyboard,在iPhone 7 size是 320x480

  3. 新增Default-568h@2x.png和Default-568h@3x.png到專案中(就算是 1x1的圖也沒關係,重點是檔名要正確),在iPhone 7 size是 320x58

2016年11月12日 星期六

iOS MapView 影像處理


import MapKit

import Foundation

extension UIImage {
    func grayScaleImage() -> UIImage {
        let imageRect = CGRect(x: 0, y: 0, width: self.size.width, height: self.size.height)
        let colorSpace = CGColorSpaceCreateDeviceGray();
        
        let width = UInt(self.size.width)
        let height = UInt(self.size.height)
        let context = CGContext(data: nil, width: Int(width), height: Int(height), bitsPerComponent: 8, bytesPerRow: 0, space: colorSpace, bitmapInfo: .allZeros);
        context?.draw(self.cgImage!, in: imageRect)
        
        let imageRef = context!.makeImage();
        let newImage = UIImage(cgImage: imageRef!)
        return newImage
    }
}

class OSMTileOverlay: MKTileOverlay {

    let cache = NSCache<NSURL, NSData>()
    let operationQueue = OperationQueue()
    
    override func url(forTilePath path: MKTileOverlayPath) -> URL {
        return URL(string: String(format: "http://tile.openstreetmap.org/%d/%d/%d.png", path.z, path.x, path.y))!
    }
    
    override func loadTile(at path: MKTileOverlayPath, result: @escaping (Data?, Error?) -> Void)  {
        let url = self.url(forTilePath: path)
        if let cachedData = cache.object(forKey: url as NSURL) as? Data {
            result(cachedData, nil)
        } else {
            let request = URLRequest(url: url)
            URLSession.shared.dataTask(with: request, completionHandler: {
                [weak self]
                data, response, error in
                if let data = data {
                    var image = UIImage(data: data)
                    image = image?.grayScaleImage()
                    let newData = UIImagePNGRepresentation(image!)
                    self?.cache.setObject((newData as? NSData)!, forKey: url as NSURL)
                    result(newData, error)
                    return
                }
                result(data, error)
            }).resume()
        }
    }

}

2016年10月22日 星期六

iOS MapViewe更換地圖來源



import MapKit

class OSMapView: MKMapView, MKMapViewDelegate {

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.setup()
        self.delegate = self;
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder);
        
        self.setup()
        self.delegate = self;
    }
    
    func setup() {
        let template = "http://tile.openstreetmap.org/{z}/{x}/{y}.png"
        let overlay = MKTileOverlay(urlTemplate: template)
        overlay.canReplaceMapContent = true
        self.add(overlay, level: .aboveLabels)
    }
    
    func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
        guard let tileOverlay = overlay as? MKTileOverlay else {
            return MKOverlayRenderer()
        }
        
        return MKTileOverlayRenderer(tileOverlay: tileOverlay)
    }

}


記得要修改info.plist
        NSExceptionDomains
       
            tile.openstreetmap.org
           
                NSExceptionAllowsInsecureHTTPLoads
               
                NSIncludesSubdomains