以下整理日常開發中常翻的十六個小技巧,涵蓋圖片處理、版本控制、座標系統、字串轉換、資料持久化與證書管理等面向。
func blurImage(value:NSNumber) -> UIImage {
let context = CIContext(options:[KCIContextUseSoftwareRenderer:true])
let ciImage = CoreImage.CIImage(image:self)
let blurFilter = CIFilter(name:"CIGassianBlur")
blurFilter?.setValue(ciImage, forKey:KCIInputImageKey)
blurFilter?.setValue(value, forKey:"inputRadius")
let imageRef = context.createCGImage((blurFilter?.outputImage)!, fromRect:(ciImage?.extent)!)
let newImage = UIImage(CGImage:imageRef)
return newImage
}
參數 value 代表模糊程度;核心就一句 CIFilter(name:"CIGaussianBlur"),透過 inputRadius 控制半徑即可。
func imageCompress(targetWidth:CGFloat) -> UIIMage {
let targetHeight = targetWidth/width*height
UIGraphicsBeginImageContext(CGSizeMake(targetWidth,targetHeight))
self.drawInRect(CGRectMake(0,0,targetWidth,targetHeight))
let newImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()
UIGrapicsEndImageContext()
return newImage
}
這裡是等比縮放,所以高度由 targetWidth/width*height 推算,避免變形。
一、SVN
A. 專案經理:1. 建立專案 — Check In;2. 設定專案成員權限
B. 成員:1. Check Out 取得完整副本(只需做一次);2. 開發;3. 階段性完成後 Commit 提交;4. 定期 Update 取得最新內容
每天下班前務必做一次可編譯的提交;每天上班第一件事是 Update。
二、Git
A. 專案經理:1. 建立專案並 Push;2. 設定成員權限
B. 成員:1. Pull 取得最新版;2. Commit 提交到本地倉庫;3. 下班前 Push 到遠端;4. 上班前 Pull 最新內容
三、M/A 狀態檔案的處理
資料夾層級的變更 SVN 支援不佳,需在工具中選定資料夾再 Commit;對應檔案則單獨選定 Commit。
過去多半做獨立開發,最多也就兩人協作,所以協作工具用得少,但一直關注這兩套程式碼託管方案,以下算是一些整理,仍待實務驗證。
iOS 9 之前:
navigationBar 若設定了背景圖片,可視座標會從 navigationBar 下方開始起算
self.navigationController?.navigationBar.setBackgroundImage(UIImage(named:"nav"), forBarMetrics:UIBarMetrics.Default)
iOS 9 之後:座標改為從狀態列下方開始。
const char *cString = "CString";
C 字串轉 NSString:NSString *str = [NSString stringWithUTF8String:cString];
NSString *str = @"NSString";
NSString 轉 C 字串:const char *cString = [str UTF8String];
Objective-C:
UILabel *label1 = ({
UILabel *label = [UILabelnew];
[self.view addSubview:label];
label.frame=CGRectMake(100,100,100,45);
label.backgroundColor= [UIColor redColor];
label.text=@"大家好1";
label;
});
UILabel*label2 = ({
UILabel*label = [UILabel new];
[self.view addSubview:label];
label.frame=CGRectMake(100,160,100,45);
label.backgroundColor= [UIColor redColor];
label.text=@"大家好2";
label;
});
UILabel*label3 = ({
UILabel*label = [UILabel new];
label.frame=CGRectMake(100,250,100,45);
label.backgroundColor= [UIColor redColor];
label.text=@"大家好3";
label;
});
[self.viewaddSubview:label3];
({
UILabel *label = [UILabel new];
[self.view addSubview:label];
label.frame=CGRectMake(100,310,100,45);
label.backgroundColor= [UIColor redColor];
label.text=@"大家好4";
});
Swift:
letlabel1:UILabel= {
let label =UILabel()
self.view.addSubview(label)
label.frame=CGRectMake(100,100,100,45)
label.backgroundColor=UIColor.redColor()
label.text="大家好"
return label
}()
let label2:UILabel= {
let label =UILabel()
label.frame=CGRectMake(100,200,100,45)
label.backgroundColor=UIColor.redColor()
label.text="大家好"
return label
}()
self.view.addSubview(label2)
語法糖的好處是複製貼上時只需改動極少地方就能達成目的。像上面這樣想建立多個 label,只要複製後改一下變數名稱就完成了。
1. FMDB 資料庫 — 需要完整的增刪改查
2. Core Data — 需要完整的增刪改查
3. 序列化(NSUserDefaults)— 狀態、偏好設定、預設選項
4. 獨立 plist — 列表型資料,如城市資訊
5. 獨立 json 檔 — 頁面列表資料
6. Realm 框架 — 需要完整的增刪改查
7. FastCoder — 某「強哥」推薦
~/Library/MobileDevice/Provisioning Profiles
平常負責多個專案的上線與開發,Mac 裡累積大量簽章檔,久了根本分不清。乾脆進到這個目錄一次清空,除錯時再讓 Xcode 重新下載當前專案需要的憑證即可。
Objective-C 版本:
- (UIViewController *)getAppRootViewController
{
UIViewController *appRootVC = [UIApplication sharedApplication].keyWindow.rootViewController;
UIViewController *topVC = appRootVC;
while (topVC.presentedViewController) {
topVC = topVC.presentedViewController;
}
return topVC;
Swift 版本:
func getAppRootViewController()->UIViewController?{
vartopVC=UIApplication.sharedApplication().keyWindow?.rootViewController
while topVC?.presentedViewController!=nil{
topVC=topVC?.presentedViewController
}
return topVC?
}
步驟:
1. 新增 iOS → Other → PCH File
2. targets → Build Setting → Prefix Header → 設定 $(SRCROOT)/檔案在專案中的路徑
3. pch 即可正常使用
例:$(SRCROOT)/FirstProject/PrefixHeader.pch
FirstProject 為 pch 路徑中的專案資料夾,PrefixHeader.pch 為檔名
當 UIViewController 作為 UINavigationController 的根視圖控制器,並把這個 Nav(root) 交給 tabBarController 時:
root.title = @"title";
那麼 self.navigationItem.title 會顯示 title,同時 nav.tabBarItem.title 也會顯示 title
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer*)gestureRecognizer {
if([gestureRecognizerisKindOfClass:[UIPanGestureRecognizerclass]]) {
CGPointvelocity = [(UIPanGestureRecognizer*)gestureRecognizervelocityInView:gestureRecognizer.view];
BOOLisHorizontalPanning =fabsf(velocity.x) >fabsf(velocity.y);
return isHorizontalPanning;
}
returnYES;
}
可在 GitHub 搜尋 UIViewController+BackButtonHandler 這個開源專案。
self.view.backgroundColor=UIColor(colorLiteralRed:255.0/255, green:255.0/255, blue:255.0/255, alpha:0.3)
let subView = UIView()
subView.tintColor=UIColor.whiteColor()//不透明了
self.view.addSubview(subView)
// 從 ipod 庫中讀出音樂檔案
MPMediaQuery*everything=[[MPMediaQueryalloc]init];
//讀取條件
MPMediaPropertyPredicate*albumNamePredicate=[MPMediaPropertyPredicatepredicateWithValue:[NSNumbernumberWithInt:MPMediaTypeMusic]forProperty:MPMediaItemPropertyMediaType];
[everythingaddFilterPredicate:albumNamePredicate];
NSLog(@"Loggingitemsfromagenericquery...");
NSArray*itemsFromGenericQuery=[everythingitems];
for(MPMediaItem*songinitemsFromGenericQuery){
NSString*songTitle=[songvalueForProperty:MPMediaItemPropertyTitle];
NSLog(@"%@",songTitle);
}
1. 如何辨識同一個應用是否安裝在同一台裝置?
2. 如何辨識同一家公司的多個應用是否安裝在同一台裝置?
問題 1 用 advertisingIdentifier 解決:同一台裝置的值相同;但使用者在「設定 → 隱私 → 廣告」關閉追蹤或回復原廠設定後就無法取得。
問題 2 用 identifierForVendor 作為企業應用標識:
com.game.yoyo 與 com.game.xoxo 在同一台裝置上,identifierForVendor 相同;
而 com.game.yoyo 與 com.buyer.yoyo 因 bundle 前綴不同,identifierForVendor 就不同。
廣告 id(每裝置唯一,重置或關閉廣告追蹤後會變更):
NSString*adId = [[[ASIdentifierManagersharedManager]advertisingIdentifier]UUIDString];
企業 id:
NSString*idfv = [[[UIDevicecurrentDevice]identifierForVendor]UUIDString];