在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 alloc ] initWithCGImage :imageRefRect];

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

#define isRetina ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640 , 960 ), [[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, nil , nil , nil );

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

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

}

6、捕获iphone通话事件:

CTCallCenter *center = [[CTCallCenter alloc ] init ];

center.callEventHandler = ^(CTCall *call )

{

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

}

7、 iOS 4 引入了多任务支持,所以用户按下 “Home” 键以后程序可能并没有退出而是转入了后台运行。如果您想让应用直接退出,最简单的方法是:在 info -plist 里面找到 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 alloc ] initWithImage :[UIImage imageNamed :@"bg.png" ]];

imageView.layer.anchorPoint = CGPointMake (0.5 , 1.0 );

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

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

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

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

}

12、更改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 ];

}

}

}

13、设置时区

NSTimeZone *defaultTimeZone = [NSTimeZone defaultTimeZone ];

NSTimeZone *tzGMT = [ NSTimeZone timeZoneWithName : @"GMT" ];

[ NSTimeZone setDefaultTimeZone:tzGMT];

上面两个时区任意用一个。

14、Ipad隐藏键盘的同时触发方法。

[[ NSNotificationCenter defaultCenter] addObserver: self

selector :@selector (keyboardWillHide:)

name : UIKeyboardWillHideNotification

object :nil ];

- (IBAction )keyboardWillHide:(NSNotification *)note

14、在一个程序中打开另一个程序的方法。

http://www.cocoachina.com/iphonedev/sdk/2010/0322/768.html

15、计算字符串的字数

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

}

16。字符串建立类

NSString* class = [[tableViewDataobjectAtIndex:indexPath.row] objectForKey:@"class"];

Class detailClass = NSClassFromString(class);

UIViewController* detailViewController = [[[detailClass alloc] initWithNibName:@"BaseViewController" bundle:nil] autorelease];


17.release 时去掉nslog的方法   
#ifndef __OPTIMIZE__
#    define NSLog(...) NSLog(__VA_ARGS__)
#else
#    define NSLog(...) {}
#endif

#ifdef DEBUG

#define DebugLog(log, ...) NSLog((@"%s [Line %d]\n\t" log), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__)

转载于:https://www.cnblogs.com/codeApp/archive/2012/11/15/2771990.html

iphone开发中的一些小技相关推荐

  1. iPhone开发中一些使用小技巧

    经过iphone开发,我发现在开发过程中最难的就是一些嘈杂的细节,而了解一些小技巧就会达到事半功倍的效果,下面我就总结一下在iphone开发中的一些小技巧. 1.如果在程序中想对某张图片进行处理的话( ...

  2. iPhone开发中的一些小技巧

    NavBar+TarBar iphone开发 NavBar+TarBar 1  改变NavBar颜色:选中Navigation Bar 的Tint属性.选中颜色. 2  隐藏"back&qu ...

  3. 学习iPhone开发中 sqlite3的使用

    由于我主要负责我们小组项目数据库模块的部分所以这几天都一直在研究在iphone中最为常用的一个简单数据库sqlite,自己也搜集很多资料,因此在这里总结一下这几天的学习成果: 1.Sqlite操作简明 ...

  4. iphone iPhone开发中如何将制作图片放大缩小代码实现案例

    1:原文摘自:http://mobile.51cto.com/iphone-285108.htm iPhone开发中如何将制作图片放大缩小案例是本文要介绍的内容,主要是来学习iphone开发中动画的制 ...

  5. iPhone开发中的技巧整理(四)

    iphone开发笔记 退回输入键盘 - (BOOL) textFieldShouldReturn:(id)textField{ [textField  resignFirstResponder]; } ...

  6. iPhone开发中现文件的增加 删除和查询

    iPhone开发中,我们常常用到一些对于文件的增加,删除和查询,这些基本的功能对于开发者来说非常的重要,而且非常实用.本文给大家介绍一下如何实现这几个具体的功能. //创建文件 -(void)Crea ...

  7. iphone iPhone开发中为UINavigationBar设置背景图片方法

    1:原文摘自:http://mobile.51cto.com/iphone-284865.htm iPhone开发中为UINavigationBar设置背景图片方法是本文要介绍的内容,在iPhone开 ...

  8. iphone开发中图像处理相关要点

    iPhone图像通常存储在以下4个地方: 相册(PhotoAlums):用户可以使用UIImagePickerController类提供的交互对话框从该相册中获取图像. 应用程序包:将图像与可执行程序 ...

  9. 拒绝内存泄露,谈一些纯代码iPhone开发中的内存管理

    笔者刚从学校出来,算上实习期,接触iPhone开发已有半载,也算是对iPhone(iOS)开发中的内存管理有了些认识,由于开发中一直没有用Interface Builder,本文就谈谈纯代码iPhon ...

最新文章

  1. linux shell 删除两个文件相同部分
  2. AWS昨日突发大规模故障,官方称受施工方挖断光纤影响
  3. 成功解决building ‘snappy._snappy‘ extension error: Microsoft Visual C++ 14.0 is required. Get it with “B
  4. mysql使用date_format格式化日期
  5. SSM+BJUI实现以Base64方式上传照片
  6. 自动感光调节LED灯亮度C语言程序,单片机PWM调节LED灯亮度
  7. 1115 Counting Nodes in a BST(甲级)
  8. bada项目在真机上调试
  9. 最适合物联网的开源数据库
  10. 平流式隔油池计算_隔油池计算方法及图集[参考提供]
  11. AI 补代码神器诞生,百度网盘克星诞生,Google 取消中国搜索引擎项目! | 开发者周刊...
  12. 计量芯片HLW8032在充电桩设备中的典型应用
  13. java输出斐波那契数列前20项,java输出斐波那契数列
  14. 知识图谱-KGE-模型:概述【KGE模型充当打分函数的作用】【负采样】【不同模型在不同KG上的表现不一致,需要尝试对比】
  15. 集线器、网桥、交换机简介
  16. 单人登录-踢下线功能实现
  17. CUDA内存管理一文理清|参加CUDA线上训练营
  18. 一份超详细的Java问题排查工具单
  19. iFunk S机械键盘游戏本登陆苏宁
  20. css 遥控器界面,怎么做成类似遥控器的上下左右控制键?

热门文章

  1. Sign In and Sign Out
  2. 【译】What do machine learning practitioners actually do?
  3. 连接不同区块链的跨链技术介绍
  4. Go从入门到精通(一)go语言初识
  5. 如何构建Android MVVM 应用框架
  6. linux内存占满但是无进程,为什么TOP看不出真实的内存占用情况?
  7. vue 箭头函数兼容性_前端学习计划之VUE学习(二)
  8. PHP随机配菜_PHP+JS三级菜单联动菜单实现方法
  9. iTunes只能装C盘吗_C盘清理简单汇总
  10. python os 常用方法_python中os常用方法详解