2011年6月27日 星期一

利用AirPrint列印UIWebView當前顯示的範圍

// Get the UIWebView content and convert to UIImage
UIViewPrintFormatter* printFormatter = [_webView viewPrintFormatter];
UIGraphicsBeginImageContext(_webView.frame.size);
[printFormatter.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// For Testing check the view layer size
//[UIImagePNGRepresentation(newImage) writeToFile:@"/Users/kito/Desktop/testfile.png" atomically:TRUE];

// Assign the AirPrint Job
UIPrintInteractionController* printController = [UIPrintInteractionController sharedPrintController];
printController.printingItem = newImage;

// Present the AirPrint Dialog from Bar Button
[printController presentFromBarButtonItem:barBtn animated:TRUE completionHandler:nil];

2011年6月26日 星期日

AirPrint Sample

AirPrint一開始還以為會很煩同複雜,
看過Document才發現,不是哪回事,
簡單幾行就要以了,IOS真係很為開發者著想。

UIPrintInteractionController* printController = [UIPrintInteractionController sharedPrintController];

UIPrintInfo *printInfo = [UIPrintInfo printInfo];
printInfo.outputType = UIPrintInfoOutputGeneral;
printInfo.jobName = @"Print Job - 1";
printController.printInfo = printInfo;
printController.printFormatter = [_webView viewPrintFormatter];

[printController presentAnimated:TRUE completionHandler:cUIPrintInteractionCompletionHandler];

2011年6月23日 星期四

MySQL一個聰明的sql,當Record沒有增加,存在會更新

今天又學了新的技巧,
所以下班了就立刻上載上Blogger做好備份,
今天的又是一個聰明的sql,
可以說是昨天的進化版。
昨天的sql沒有才會加入Record,
不過今天就是加了新功能,
存在Record的會自動更新
代碼如下:

// INSERT代碼
INSERT INTO tabTesting (user, token)
VALUES ('$user', '$token')
// 關鍵所在ON DUPLICATE KEY,即是Column為Key時,加入的Data重複了就會運行以下代碼
ON DUPLICATE KEY
// Update代碼
UPDATE user='$user';

PS. 假如你的Table本身有了Primary Key Column哪怎辦?
其實你可以把你想做檢測Data的Column做成Unique Key,
SQL就會懂得幫你去檢測了 XD

2011年6月21日 星期二

MySQL一個聰明的sql,當Record沒有的時候才增加

今天幫Project做maintain,
發現同事寫的一行SQL代碼,我不太明白,
細問之下原來功用十分奇妙!!
學到新野,我梗係即時記下來,
就是insert時,當Record沒有的時候才insert一個新的,
大大簡化了一個Coding上的一大段檢測代碼。
代碼如下:

// insert command
insert into myfavorite (songname,songid,singername,singertype)
// select command用作制造一個visual record
select 'MAKE IT REAL','G023203','(null)',0
// where用作檢測not exists的結果
where not exists
(
    // select相同的table,record存在就return 1,不存在為NULL
    select 1 from myfavorite
    where songid = 'G023203'
);

iOS App進入Background Process也可不中斷(二)

之前一次已經說過iOS App進入Background Process也可不中斷連線下載的方法,
不過上次的方法有一個壞處,
就是用戶再開啟App,App是不會更新畫面,
只會出Default Image要直到Callback完成
所以今天看了一下Help Document,
終於找到最好的方法了

UIBackgroundTaskIdentifier bgTask;
if(bgTask == UIBackgroundTaskInvalid)
{
    UIApplication* app = [UIApplication sharedApplication];

    // 開啟了BackgroundTask就要以令以下的queue在Background/Foreground Task都可以運行
    bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
        NSLog(@"System Expiration End Background Task");
        [app endBackgroundTask:bgTask];
        bgTask = UIBackgroundTaskInvalid;
    }];

    // Start the long-running task and return immediately.
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

    // Do the work associated with the task.
    for (int i = 0; i < 1000; i++)
    {
        NSLog(@"Sleep %d", i);
        // 返回Main Process更新UI
        dispatch_async(dispatch_get_main_queue(), ^{
            UIButton* btn = (UIButton*)[viewController.view viewWithTag:100];
            [btn setTitle:[NSString stringWithFormat:@"Sleep %d", i] forState:UIControlStateNormal];
        }); 
        [NSThread sleepForTimeInterval:5];
    }

    NSLog(@"Completed State End Background Task");
    [app endBackgroundTask:bgTask];
    bgTask = UIBackgroundTaskInvalid;
    });
}

UISearchBar小技巧(二)-修改UISearchBar內其他完件的大小

今天有項目要把UISearchBar放大,
不過只係設定UISearchBar的Frame Size就只會把UISearchBar背景放大,
而中間的Text Field沒有放大,
於是又用到找Subview的方法,
找出Text Field來放大,
不過要放大UISearchBar內其他完件的大小,
不可以在平時的Method Call完成,
要用到Override UIView下layoutSubviews method才可以完成

@implementation UISearchBar (Override)

- (void)layoutSubviews
{
   UITextField *searchField;
   for(int i = 0; i < [self.subviews count]; i++)
   {
      if([[self.subviews objectAtIndex:i] isKindOfClass:[UITextField class]])
      {
         searchField = [self.subviews objectAtIndex:i];
      }
   }
}

@end

2011年6月12日 星期日

UISearchBar小技巧

今早有朋友問我如何令UISearchBar的Keyboard彈出,
不過早上太多工作沒時間回他們,
所以現在Lunch時間寫出來,
給他們看看,方便我不用在MSN上說多一次

UISearchBar* searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];

// 獲取所有UISearchBar Subviews的內容
//NSLog(@"%@", [searchBar.subviews description]);

// 令UISearchBar的Keyboard彈出
[((UIControl*)[searchBar.subviews objectAtIndex:1]) becomeFirstResponder];

// 令UISearchBar沒有了背景
[((UIView*)[searchBar.subviews objectAtIndex:0]) removeFromSuperview];

[self.view addSubview:searchBar];
[searchBar release];

iOS劃分Debug/Release代碼的方法

有時寫代碼的時候有一些Log信息只想在Debug Mode下才顯示,
哪麼以下代碼就很合適了

#ifdef __OPTIMIZE__
// Release Mode
#else
// Debug Mode
#endif

2011年6月10日 星期五

讓MKMapView懂得自動移去兩個地圖子針之間

今天Project要加入一個新功能,
就是令MKMapView懂得自動移去兩個地圖子針之間的位置,
令用戶可以知道目的地同自己差多遠,
最後看了一下MKMapView的Document就找到了,
倒不算太難,
留下來作筆記XD

// 獲取用戶的位置範圍
MKMapPoint userMapPoint = MKMapPointForCoordinate(userLocation.location.coordinate);
MKMapRect userRect = MKMapRectMake(userMapPoint.x, userMapPoint.y, 50, 50);

// 獲取目的地的位置範圍
MKMapPoint targetMapPoint = MKMapPointForCoordinate(_coordinate);
MKMapRect targetRect = MKMapRectMake(targetMapPoint.x, targetMapPoint.y, 50, 50);

// 把範圍Union
MKMapRect unionRect = MKMapRectUnion(userRect, targetRect);
// 叫MKMapView移去指定範圍
[_mapView setVisibleMapRect:unionRect animated:TRUE];

2011年6月8日 星期三

令iOS App進入Background Process也可以進行連線下載

其實要令到iOS App進入Background Process也可以進行連線下載不中斷,
其實有一個很簡單的方法,
就是用NSTimer啦!!
利用NSTimer,在NSTimer的Callback Function,
即是App進入Background Process,
iOS還是會讓NSTimer的Callback Function完成,
才會在Background Process停下來。

PS:不過這方法有一個問題,就是NSTimer的Callback沒完成,用戶再開啟App,App是不會更新畫面,只會出Default Image要直到Callback完成,才會更新

[NSTimer scheduledTimerWithTimeInterval:0.1f target:self selector:@selector(timerLoadUpdate) userInfo:nil repeats:FALSE];