2016年12月1日 星期四
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 MapKitclass 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 = trueself.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
2016年8月18日 星期四
swift + crc32
class CRC32 {
var data = NSData()private static let crcTableLength = Int(UInt8.max) + 1private static var _crcTable = [UInt32](count: crcTableLength, repeatedValue: 0)private static var crcTable: [UInt32] {get {if _crcTable[1] != 0 {return _crcTable}for n in 0..var c = UInt32(n)for _ in 0..<8 {if ((c & 1) != 0) {c = 0xedb88320 ^ (c >> 1)} else {c = c >> 1}}_crcTable[n] = c}return _crcTable}}func getCRC() -> Byte4 {var crc: Byte4 = 0crc = ~crcvar buf = UnsafePointer<UInt8>(data.bytes)let len = data.lengthfor _ in 0..let toBuf = buf.memorybuf += 1let i = Int((crc ^ UInt32(toBuf)) & 0xFF)crc = self.dynamicType.crcTable[i] ^ crc >> 8}return ~crc}}
2016年8月9日 星期二
HTML+UILabel(or TextView)
On swift
var attrStr = try! NSAttributedString(
data: "text".dataUsingEncoding(NSUnicodeStringEncoding, allowLossyConversion: true)!,
options: [ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],
documentAttributes: nil)
label.attributedText = attrStr
On java
myTextView.setText(Html.fromHtml("
Description here
"));
Title
Description here
"));
2016年7月30日 星期六
swift script
沒想到swift也可以替代shell script
#!/usr/bin/swift -F Carthage/Build/Mac/
#只要開頭像這樣宣告就可以用swift xxx.swift執行
#另外 -F 是第三方framework的路徑
#!/usr/bin/env xcrun --toolchain "com.apple.dt.toolchain.Swift_2_3" --sdk machos swift
#使用特殊版本的swift
#請愛用carthage
#mkdir swift_btc_script
#cd swift_btc_script
#echo 'github "nomothetis/OptionKit" ~> 1.0' > Cartfile
#carthage update
#!/usr/bin/swift -F Carthage/Build/Mac/
#只要開頭像這樣宣告就可以用swift xxx.swift執行
#另外 -F 是第三方framework的路徑
#!/usr/bin/env xcrun --toolchain "com.apple.dt.toolchain.Swift_2_3" --sdk machos swift
#使用特殊版本的swift
#請愛用carthage
#mkdir swift_btc_script
#cd swift_btc_script
#echo 'github "nomothetis/OptionKit" ~> 1.0' > Cartfile
#carthage update
2016年7月1日 星期五
swift強化版的enum
enum 水果 {case 櫻桃(價位: Int, 產地: String)case 火龍果(價位: Int, 產地: String)static let 價目表 = [櫻桃(價位: 3400, 產地: "華盛頓"),火龍果(價位: 1100, 產地: "埔里")]private func valueAt(pos: Int) -> AnyObject? {if let first = Mirror(reflecting: self).children.first {let children = Mirror(reflecting: first.value).childrenlet index = children.startIndex.advancedBy(IntMax(pos))if let value = children[index].value as? AnyObject {return value}}return nil}func 價位() -> Int {if let value = valueAt(0) as? Int {return value}return 0}private func valueAt(pos: Int, with: String) -> AnyObject? {if let first = Mirror(reflecting: self).children.first {if first.label == with {let children = Mirror(reflecting: first.value).childrenlet index = children.startIndex.advancedBy(IntMax(pos))if let value = children[index].value as? AnyObject {return value}}}return nil}static func 詢價(水果名: String) -> Int {for 價目 in 價目表 {if let price = 價目.valueAt(0, with: 水果名) as? Int {return price}}return 0}}水果.詢價("火龍果")
2016年3月24日 星期四
2016年2月25日 星期四
use apple script to disable AppleUSBFTDI
tell application "Finder"
display dialog ("Check dongle is plugin.") buttons {"Ok"}
try
do shell script "sudo kextunload -b com.apple.driver.AppleUSBFTDI" with administrator privileges
end try
display dialog ("It work!") buttons {"Ok"}
end tell
訂閱:
文章 (Atom)