0. NSLog的調(diào)試
#ifdef __OBJC__
#ifdef DEBUG
#define NSLog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__)
#else
#define NSLog(...)
#endif
#endif
1. 退回輸入鍵盤(pán)
- (BOOL) textFieldShouldReturn:(id)textField{ [textField resignFirstResponder];
}
2. CGRect
frame = CGRectMake (origin.x, origin.y, size.width, size.height);矩形
NSStringFromCGRect(someCG) 把 CGRect 結(jié)構(gòu)轉(zhuǎn)變?yōu)楦袷交址?
CGRectFromString(aString) 由字符串恢復(fù)出矩形;
CGRectInset(aRect) 創(chuàng)建較小或較大的矩形(中心點(diǎn)相同),+較小 -較大
CGRectIntersectsRect(rect1, rect2) 判斷兩矩形是否交叉,是否重疊
CGRectZero 高度和寬度為零的/位于(0,0)的矩形常量
3. CGPoint & CGSize
CGPoint aPoint = CGPointMake(x, y);
CGSize aSize = CGSizeMake(width, height);
4. 設(shè)置透明度
[myView setAlpha:value]; (0.0 < value < 1.0)
5. 自定義顏色
UIColor *newColor = [[UIColor alloc] initWithRed:(float) green:(float) blue:(float) alpha:(float)];
0.0~1.0
6. 屏幕
豎屏: 320X480
橫屏: 480X320
橫屏: [[UIApplication shareApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight].
屏幕變動(dòng)檢測(cè): orientation == UIInterfaceOrientationLandscapeLeft
7. 隱藏狀態(tài)欄
[[UIApplication shareApplication] setStatusBarHidden: YES
animated:NO]
8. 自動(dòng)適應(yīng)父視圖大小:
aView.autoresizingSubviews = YES;
aView.autoresizingMask = (UIViewAutoresizingFlexibleWidth |
UIViewAutoresizingFlexibleHeight);
9. 圖片壓縮
壓縮圖片
用法:UIImage *yourImage= [self imageWithImageSimple:image scaledToSize:CGSizeMake(210.0, 210.0)];
壓縮圖片
- (UIImage*)imageWithImageSimple:(UIImage*)image scaledToSize:(CGSize)newSize
{
Create a graphics image context
UIGraphicsBeginImageContext(newSize);
Tell the old image to draw in this newcontext, with the desired
new size
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
Get the new image from the context
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
End the context
UIGraphicsEndImageContext();
Return the new image.
return newImage;
}
10. 對(duì)圖庫(kù)的操作
- 1, 選擇相冊(cè):
UIImagePickerControllerSourceTypesourceType=UIImagePickerControllerSourceTypeCamera;
if (![UIImagePickerControllerisSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
}
UIImagePickerController * picker = [[UIImagePickerControlleralloc]init];
picker.delegate = self;
picker.allowsEditing=YES;
picker.sourceType=sourceType;
[self presentModalViewController:picker animated:YES];
- 2, 選擇完畢:
-(void)imagePickerController:(UIImagePickerController*)pickerdidFinishPickingMediaWithInfo:(NSDictionary *)info
{
[picker dismissModalViewControllerAnimated:YES];
UIImage * image=[info objectForKey:UIImagePickerControllerEditedImage];
[self performSelector:@selector(selectPic:) withObject:imageafterDelay:0.1];
}
-(void)selectPic:(UIImage*)image
{
NSLog(@"image%@",image);
imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(0, 0, image.size.width, image.size.height);
[self.viewaddSubview:imageView];
[self performSelectorInBackground:@selector(detect:) withObject:nil];
}
-3, 取消選擇:
-(void)imagePickerControllerDIdCancel:(UIImagePickerController*)picker
{
[picker dismissModalViewControllerAnimated:YES];
}
11. Status Bar操作
隱藏Status Bar
[[UIApplication sharedApplication]setStatusBarHidden:YES animated:NO];
狀態(tài)欄的網(wǎng)絡(luò)活動(dòng)風(fēng)火輪是否旋轉(zhuǎn)
[UIApplication sharedApplication].networkActivityIndicatorVisible,默認(rèn)值是NO。
12. 鍵盤(pán)透明
textField.keyboardAppearance = UIKeyboardAppearanceAlert;
13. 截取屏幕圖片
創(chuàng)建一個(gè)基于位圖的圖形上下文并指定大小為
CGSizeMake(200,400)
UIGraphicsBeginImageContext(CGSizeMake(200,400));
renderInContext 呈現(xiàn)接受者及其子范圍到指定的上下文
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
返回一個(gè)基于當(dāng)前圖形上下文的圖片
UIImage *aImage = UIGraphicsGetImageFromCurrentImageContext();
移除棧頂?shù)幕诋?dāng)前位圖的圖形上下文
UIGraphicsEndImageContext();
以png格式返回指定圖片的數(shù)據(jù)
imageData = UIImagePNGRepresentation(aImage);
14. 修改PlaceHolder的默認(rèn)顏色
[username_text setValue:[UIColor colorWithRed:1 green:1 blue:1 alpha:0.5] forKeyPath:@"_placeholderLabel.textColor"];
15. 頁(yè)面上移解決文本框被鍵盤(pán)彈出擋住的問(wèn)題
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[username_text resignFirstResponder];
[password_text resignFirstResponder];
When the user presses return, take focus away from the text field so that the keyboard is dismissed.
NSTimeInterval animationDuration = 0.30f;
[UIView beginAnimations:@"ResizeForKeyboard" context:nil];
[UIView setAnimationDuration:animationDuration];
CGRect rect = CGRectMake(0.0f, 0.0f, self.view.frame.size.width, self.view.frame.size.height);
self.view.frame = rect;
[UIView commitAnimations];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
When the user presses return, take focus away from the text field so that the keyboard is dismissed.
NSTimeInterval animationDuration = 0.30f;
[UIView beginAnimations:@"ResizeForKeyboard" context:nil];
[UIView setAnimationDuration:animationDuration];
CGRect rect = CGRectMake(0.0f, 0.0f, self.view.frame.size.width, self.view.frame.size.height);
self.view.frame = rect;
[UIView commitAnimations];
[textField resignFirstResponder];
return YES;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
CGRect frame = password_text.frame;
int offset = frame.origin.y + 32 - (self.view.frame.size.height - 216.0);//鍵盤(pán)高度216
NSTimeInterval animationDuration = 0.30f;
[UIView beginAnimations:@"ResizeForKeyBoard" context:nil];
[UIView setAnimationDuration:animationDuration];
float width = self.view.frame.size.width;
float height = self.view.frame.size.height;
if(offset > 0)
{
CGRect rect = CGRectMake(0.0f, -offset,width,height);
self.view.frame = rect;
}
[UIView commitAnimations];
}
16. iOS代碼加密常用加密方式
- 1, MD5加密
.h中
#import <Foundation/Foundation.h>
@interface CJMD5 : NSObject
+(NSString *)md5HexDigest:(NSString *)input;
@end
-------------------------------------------
.m中
#import "CJMD5.h"
#import <CommonCrypto/CommonDigest.h>
@implementation CJMD5
+(NSString *)md5HexDigest:(NSString *)input{
const char* str = [input UTF8String];
unsigned char result[CC_MD5_DIGEST_LENGTH];
CC_MD5(str, strlen(str), result);
NSMutableString *ret = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH];
for(int i = 0; i<CC_MD5_DIGEST_LENGTH; i++) {
[ret appendFormat:@"%02X",result];
}
return ret;
}
@end
----------------------------------------------------------------
MD5是不可逆的只有加密沒(méi)有解密,iOS代碼加密使用方式如下
NSString *userName = @"cerastes";
NSString *password = @"hello Word";
MD5加密
NSString *md5 = [CJMD5 md5HexDigest:password];
NSLog(@"%@",md5);
END
- 2, AES加密
NSString *encryptedData = [AESCrypt encrypt:userName password:password];加密
NSString *message = [AESCrypt decrypt:encryptedData password:password]; 解密
NSLog(@"加密結(jié)果 = %@",encryptedData);
NSLog(@"解密結(jié)果 = %@",message);
- 3, BASE64加密iOS代碼加密
.h
+ (NSString*)encodeBase64String:(NSString *)input;
+ (NSString*)decodeBase64String:(NSString *)input;
+ (NSString*)encodeBase64Data:(NSData *)data;
+ (NSString*)decodeBase64Data:(NSData *)data;
.m
+ (NSString*)encodeBase64String:(NSString * )input {
NSData *data = [input dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
data = [GTMBase64 encodeData:data];
NSString *base64String = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
return base64String;
}
+ (NSString*)decodeBase64String:(NSString * )input {
NSData *data = [input dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
data = [GTMBase64 decodeData:data];
NSString *base64String = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
return base64String;
}
// 加密
+ (NSString*)encodeBase64Data:(NSData *)data {
data = [GTMBase64 encodeData:data];
NSString *base64String = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
return base64String;
}
// 加密
+ (NSString*)decodeBase64Data:(NSData *)data {
data = [GTMBase64 decodeData:data];
NSString *base64String = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
return base64String;
}
BASE64加密
NSString *baseEncodeString = [GTMBase64 encodeBase64String:password];
NSString *baseDecodeString = [GTMBase64 decodeBase64String:baseEncodeString];
NSLog(@"baseEncodeString = %@",baseEncodeString);
NSLog(@"baseDecodeString = %@",baseDecodeString);
17. 版本比較
+ (BOOL)isVersion:(NSString*)versionA biggerThanVersion:(NSString*)versionB
{
NSArray *arrayNow = [versionB componentsSeparatedByString:@"."];
NSArray *arrayNew = [versionA componentsSeparatedByString:@"."];
BOOL isBigger = NO;
NSInteger i = arrayNew.count > arrayNow.count? arrayNow.count : arrayNew.count;
NSInteger j = 0;
BOOL hasResult = NO;
for (j = 0; j < i; j ++) {
NSString* strNew = [arrayNew objectAtIndex:j];
NSString* strNow = [arrayNow objectAtIndex:j];
if ([strNew integerValue] > [strNow integerValue]) {
hasResult = YES;
isBigger = YES;
break;
}
if ([strNew integerValue] < [strNow integerValue]) {
hasResult = YES;
isBigger = NO;
break;
}
}
if (!hasResult) {
if (arrayNew.count > arrayNow.count) {
NSInteger nTmp = 0;
NSInteger k = 0;
for (k = arrayNow.count; k < arrayNew.count; k++) {
nTmp += [[arrayNew objectAtIndex:k]integerValue];
}
if (nTmp > 0) {
isBigger = YES;
}
}
}
return isBigger;
}
18. setValue: forKey:的定義
@interface NSMutableDictionary(NSKeyValueCoding)
/* Send -setObject:forKey: to the receiver, unless the value is nil, in which case send -removeObject:forKey:.
*/
- (void)setValue:(id)value forKey:(NSString *)key;
@end
value 為 nil ,調(diào)用 removeObject:forKey:
value不為nil時(shí)調(diào)用 setObject:forKey:
key為NSString類型。
2 setObject:forKey:的定義
@interface NSMutableDictionary : NSDictionary
- (void)removeObjectForKey:(id)aKey;
- (void)setObject:(id)anObject forKey:(id <NSCopying>)aKey;
@end
anobject不能為nil,而且key是一個(gè)id類型,不僅限于NSString類型
兩者的區(qū)別:
- (1)setObject:forkey:中value是不能夠?yàn)閚il的;setValue:forKey:中value能夠?yàn)閚il,但是當(dāng)value為nil的時(shí)候,會(huì)自動(dòng)調(diào)用removeObject:forKey方法
- (2)setValue:forKey:中key只能夠是NSString類型,而setObject:forKey:的可以是任何類型
待續(xù)...