-------------------------------------------------------------- html
一、 若是在程序中想對某張圖片進行處理的話(獲得某張圖片的一部分)可一用如下代碼: ios
UIImage *image = [UIImage imageNamed:filename]; CGImageRef imageRef = image.CGImage; CGRect rect = CGRectMake(origin.x, origin.y ,size.width, size.height); CGImageRef imageRefRect = CGImageCreateWithImageInRect(imageRef, rect); UIImage *imageRect = [[UIImage alloc] initWithCGImage:imageRefRect];
二、判斷設備是iphone仍是iphone4的代碼: windows
#define isRetina ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 960), [[UIScreen mainScreen] currentMode].size) : NO)
三、判斷郵箱輸入的是否正確: xcode
- (BOOL) validateEmail: (NSString *) candidate { NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}"; NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex]; return [emailTest evaluateWithObject:candidate]; }
四、如何把當前的視圖做爲照片保存到相冊中去: app
#import <QuartzCore/QuartzCore.h> UIGraphicsBeginImageContext(currentView.bounds.size); //currentView 當前的view [currentView.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);
五、本地通知(相似於push通知)按home鍵到後臺 十秒後觸發: iphone
UILocalNotification *notification=[[UILocalNotification alloc] init]; if (notification!=nil) { NSLog(@">> support local notification"); NSDate *now=[NSDate new]; notification.fireDate=[now addTimeInterval:10]; notification.timeZone=[NSTimeZone defaultTimeZone]; notification.alertBody=@"該去吃晚飯了!"; [[UIApplication sharedApplication].scheduleLocalNotification:notification];
六、捕獲iphone通話事件: ide
CTCallCenter *center = [[CTCallCenter alloc] init]; center.callEventHandler = ^(CTCall *call) { NSLog(@"call:%@", call.callState); }
七、iOS 4 引入了多任務支持,因此用戶按下 「Home」 鍵之後程序可能並無退出而是轉入了後臺運行。若是您想讓應用直接退出,最簡單的方法是:在 info-plist 裏面找到 Application does not run in background 一項,勾選便可。 ui
八、使UIimageView的圖像旋轉: this
float rotateAngle = M_PI; CGAffineTransform transform=CGAffineTransformMakeRotation(rotateAngle); imageView.transform = transform;
九、設置旋轉的原點: lua
#import <QuartzCore/QuartzCore.h> UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg.png"]]; imageView.layer.anchorPoint = CGPointMake(0.5, 1.0);
十、實現自定義的狀態欄(遮蓋狀態欄):
CGRect frame = {{0, 0}, {320, 20}}; UIWindow* wd = [[UIWindow alloc] initWithFrame:frame]; [wd setBackgroundColor:[UIColor clearColor]]; [wd setWindowLevel:UIWindowLevelStatusBar]; frame = CGRectMake(100, 0, 30, 20); UIImageView* img = [[UIImageView alloc] initWithFrame:frame]; [img setContentMode:UIViewContentModeCenter]; [img setImage:[UIImage imageNamed:@"00_0103.png"]]; [wd addSubview:img]; [wd makeKeyAndVisible]; [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:2]; frame.origin.x += 150; [img setFrame:frame]; [UIView commitAnimations];
十一、在程序中實現電話的撥打:
//添加電話圖標按鈕 UIButton *btnPhone = [[UIButton buttonWithType:UIButtonTypeCustom] retain]; btnPhone.frame = CGRectMake(280,10,30,30); UIImage *image = [UIImage imageNamed:@"phone.png"]; [btnPhone setBackgroundImage:image forState:UIControlStateNormal]; //點擊撥號按鈕直接撥號 [btnPhone addTarget:self action:@selector(callAction:event:)forControlEvents:UIControlEventTouchUpInside]; [cell.contentView addSubview:btnPhone]; //cell是一個UITableViewCell //定義點擊撥號按鈕時的操做 - (void)callAction:(id)sender event:(id)event{ NSSet *touches = [event allTouches]; UITouch *touch = [touches anyObject]; CGPoint currentTouchPosition = [touch locationInView:self.listTable]; NSIndexPath *indexPath = [self.listTable indexPathForRowAtPoint: currentTouchPosition]; if (indexPath == nil) { return; } NSInteger section = [indexPath section]; NSUInteger row = [indexPath row]; NSDictionary *rowData = [datas objectAtIndex:row]; NSString *num = [[NSString alloc] initWithFormat:@"tel://%@",number]; //number爲號碼字符串 [[UIApplication sharedApplication] openURL:[NSURL URLWithString:num]]; //撥號 }
十二、更改iphone的鍵盤顏色:
1.只有這2種數字鍵盤纔有效果。UIKeyboardTypeNumberPad,UIKeyboardTypePhonePad
2. keyboardAppearance = UIKeyboardAppearanceAlert
- (void)textViewDidBeginEditing:(UITextView *)textView{ NSArray *ws = [[UIApplication sharedApplication] windows]; for(UIView *w in ws){ NSArray *vs = [w subviews]; for(UIView *v in vs) { if([[NSString stringWithUTF8String:object_getClassName(v)] isEqualToString:@"UIKeyboard"]) { v.backgroundColor = [UIColor redColor]; } } }
1三、設置時區
NSTimeZone *defaultTimeZone = [NSTimeZone defaultTimeZone]; NSTimeZone *tzGMT = [NSTimeZone timeZoneWithName:@"GMT"]; [NSTimeZone setDefaultTimeZone:tzGMT];
上面兩個時區任意用一個。
1四、Ipad隱藏鍵盤的同時觸發方法。
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil]; - (IBAction)keyboardWillHide:(NSNotification *)note
1五、在一個程序中打開另外一個程序的方法。
http://www.cocoachina.com/iphonedev/sdk/2010/0322/768.html
1六、計算字符串的字數
-(int)calculateTextNumber:(NSString *)text { float number = 0.0; int index = 0; for (index; index < [text length]; index++) { NSString *protoText = [text substringToIndex:[text length] - index]; NSString *toChangetext = [text substringToIndex:[text length] -1 -index]; NSString *charater; if ([toChangetext length]==0) { charater = protoText; } else{ NSRange range = [text rangeOfString:toChangetext]; charater = [protoText stringByReplacingCharactersInRange:range withString:@""]; } NSLog(charater); if ([charater lengthOfBytesUsingEncoding:NSUTF8StringEncoding] == 3) { number++; } else { number = number+0.5; } } return ceil(number); }
--------------------------------------------------------------
在終端用下述命令添加模板到xcode中: sudo path/install-templates.sh -f
說明1:其中,sudo 爲獲取root權限,path爲cocos2d路徑+解壓後的文件名,-f表示覆蓋已經存在的cocos2d模板。
說明2:若是已經或去過root權限,再次添加模板不用添加 sudo。添加也無所謂,終端會有錯誤提示,再改就是,嘿嘿!~
問題描述:用最基礎的cocos2d模板建立項目以後,編譯成功,可是沒法運行,提示「choose a destination with a supported architecture in order to run on this device. xcode 5」。
網上也有相似錯誤,解決方法包括:檢查項目文件名是否有空格;檢查app所需系統版本是否高於Xcode能運行的版本;可是這些問題在樓主的新建項目中都不存在。
問題解決:最後決定,先跳過運行這一步,直接「convert project to ARC」。而後,提示有19個錯誤,檢查以後發現都是,建立自動釋放池,id前加*,或者使用了被棄用的方法。
轉換以後,再點擊運行,終於看到helloword界面。雖然對着mac本本有四個月,而第一次使用xcode也是一年前的事了,可是始終都對mac和xoce心存畏懼,唉,要抓緊時間了。
問題描述:
Ld /Users/marong/Library/Developer/Xcode/DerivedData/HelloWord2-ddijqbonjxfttwbhiexrtaphvgfq/Build/Products/Debug-iphonesimulator/HelloWord2.app/HelloWord2 normal i386 cd /Users/marong/project/Cocos2d/HelloWord2 setenv IPHONEOS_DEPLOYMENT_TARGET 6.1 setenv PATH "/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin:/Applications/Xcode.app/Contents/Developer/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin" /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang -arch i386 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator7.0.sdk -L/Users/marong/Library/Developer/Xcode/DerivedData/HelloWord2-ddijqbonjxfttwbhiexrtaphvgfq/Build/Products/Debug-iphonesimulator -F/Users/marong/Library/Developer/Xcode/DerivedData/HelloWord2-ddijqbonjxfttwbhiexrtaphvgfq/Build/Products/Debug-iphonesimulator -filelist /Users/marong/Library/Developer/Xcode/DerivedData/HelloWord2-ddijqbonjxfttwbhiexrtaphvgfq/Build/Intermediates/HelloWord2.build/Debug-iphonesimulator/HelloWord2.build/Objects-normal/i386/HelloWord2.LinkFileList -Xlinker -objc_abi_version -Xlinker 2 -lz -fobjc-link-runtime -Xlinker -no_implicit_dylibs -mios-simulator-version-min=6.1 -lcocos2d-library -framework OpenAL -framework UIKit -framework QuartzCore -framework AVFoundation -framework CoreGraphics -framework OpenGLES -framework Foundation -framework AudioToolbox -Xlinker -dependency_info -Xlinker /Users/marong/Library/Developer/Xcode/DerivedData/HelloWord2-ddijqbonjxfttwbhiexrtaphvgfq/Build/Intermediates/HelloWord2.build/Debug-iphonesimulator/HelloWord2.build/Objects-normal/i386/HelloWord2_dependency_info.dat -o /Users/marong/Library/Developer/Xcode/DerivedData/HelloWord2-ddijqbonjxfttwbhiexrtaphvgfq/Build/Products/Debug-iphonesimulator/HelloWord2.app/HelloWord2 ld: library not found for -lcocos2d-library clang: error: linker command failed with exit code 1 (use -v to see invocation)
感受應該是sdk的版本不對。運行在模擬器中給定的是7.0的版本,可是項目要求的是6.1。到網上搜了一下 添加舊sdk版本到xcode中的方法。
copy the iPhoneOS5.1.sdk to the directory /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS5.1.sdk and iPhoneSimulator5.1.sdk to /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator5.1.sdk
問題解決:明天早上繼續。。。
--------------------------------------------------------------