iphone开发中的一些小技巧

1、 如果在程序中想对某张图片进行处理的话(得到某张图片的一部分)可一用以下代码:
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 allocinitWithCGImage:imageRefRect];


2、判断设备是iphone还是iphone4的代码:


#define isRetina ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640960), [[UIScreen mainScreen] currentMode].size) : NO)


3、判断邮箱输入的是否正确:


- (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];

}


4、如何把当前的视图作为照片保存到相册中去:

 

#import <QuartzCore/QuartzCore.h>

UIGraphicsBeginImageContext(currentView.bounds.size);     //currentView 当前的view

[currentView.layer renderInContext:UIGraphicsGetCurrentContext()];

UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

UIImageWriteToSavedPhotosAlbum(viewImage, nilnilnil);


5、本地通知(类似于push通知)按home键到后台 十秒后触发:

UILocalNotification *notification=[[UILocalNotification allocinit]; 

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];

}


6、捕获iphone通话事件:

CTCallCenter *center = [[CTCallCenter allocinit];

center.callEventHandler = ^(CTCall *call

{

NSLog(@"call:%@"call.callState);

}


7、iOS 4 引入了多任务支持,所以用户按下 “Home” 键以后程序可能并没有退出而是转入了后台运行。如果您想让应用直接退出,最简单的方法是:在 infoplist 里面找到 Application does not run in background 一项,勾选即可。


8、使UIimageView的图像旋转:


float rotateAngle = M_PI;

CGAffineTransform transform =CGAffineTransformMakeRotation(rotateAngle);

imageView.transform = transform;


9、设置旋转的原点:

 

#import <QuartzCore/QuartzCore.h>

UIImageView *imageView = [[UIImageView allocinitWithImage:[UIImage imageNamed:@"bg.png"]];

imageView.layer.anchorPoint = CGPointMake(0.51.0);


10、实现自定义的状态栏(遮盖状态栏):

CGRect frame = {{00}, {32020}};

UIWindow* wd = [[UIWindow allocinitWithFrame:frame];

[wd setBackgroundColor:[UIColor clearColor]];

[wd setWindowLevel:UIWindowLevelStatusBar];

frame = CGRectMake(10003020);

UIImageView* img = [[UIImageView allocinitWithFrame: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];


11、在程序中实现电话的拨打:


//添加电话图标按钮 

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 allocinitWithFormat:@"tel://%@",number]; //number为号码字符串     

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:num]]; //拨号 

}


12、更改iphone的键盘颜色:


1.只有这2种数字键盘才有效果。UIKeyboardTypeNumberPadUIKeyboardTypePhonePad

2. keyboardAppearance  UIKeyboardAppearanceAler

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值