iphone开发小技巧汇总(1)

1.iphone之xx_Prefix.pch的作用和用法

    Prefix.pch:扩展名.pch表示"pre-compiled-header",这是一个你工程要用到的来自于外部框架的头文件列表。xcode将编译这些头到文件,这将减少你在选择Build或Build and Go时编译项目的时间。通常用到的头文件已经自动包含了

   pch,系统编译每个.m文件前,都会先import这个文件。这样既节省了你手动添加import的时间,也有助于加速编译.

   还有就是可以在这里面放入宏,在整个工程中都可以用。节省了时间

2.iphone之发布版本的时候移除NSLog输出的方法

只需要将下列代码加入到pch文件中即可, __OPTIMIZE__这个编译选项是发布版本才有的,所以在编译调试版本的时候可以看到Log,而发布版本则没有Log。

#ifndef __OPTIMIZE__

   #define NSLog(...) NSLog(__VA_ARGS__)

#else

   #define NSLog(...) {}

#endif

3.iphone之取消icon的高光状态

系统会默认会在图标上 自动加上半透明的高光半圆,如果我们不想要这个效果或者图标本身已经包含了这个高光效果,我们可以在项目配置里把系统的高光功能取消掉:

xcode3.2x建的项目:

在info plist里加一个配置项,key为“Icon already includes gloss and bevel effects”, 类型为bool,然后打上钩就,这样系统就不会自动加高光;
 

xcode4建的项目:

在项目target的summary标签页下找到App Icons项,在“Prerendered”打上钩
 

 
此时在info.plist里会多出一个配置项" Icon already includes gloss effects":

 
再找到 “Icon files (iOS 5)”项目(如果有的话),展开, 把里面的“Icon already includes gloss effects”也设置成“YES”

这样程序中的高光效果就取消了。

4.iphone之unichar和初始化

在iphone/mac开发中,unichar是两字节长的char,代表unicode的一个字符。但在xcode中,初始化unichar是个问题。如果像下面这样声明,会有warning"Multi-character character constant"。

unichar a = '国';

这是因为C语言中两个单引号只能用于char。可以采用直接写文字编码的方式来初始化。

unichar a = 0x0100;

如果有很多个unichar怎么办?一个个去查表太麻烦了。可以采取变通的方法:

unichar a[10];

NSString *aString = @"一二三四五六七八九十";

for (int i = 0; i < 10; i++)

a[i] = [aString characterAtIndex:i];

5.iphone之转向appstore中商品

NSString *str =@"itms-apps://itunes.apple.com/WebObjects/MZStore.woa/wa/viewSoftware? id=473898949";

[[UIApplication sharedApplication] openURL:[NSURLURLWithString:str]];

把"id=3410863403"换成id=xxxx(自己程序的ID),即可。

6.iphone之iphone控件尺寸


iphone开发小技巧汇总(1)_第1张图片
Element Size (in points)
Window (including status bar) 320 x 480 pts
Status Bar
(How to hide the status bar)
20 pts
View inside window (visible status bar) 320 x 460
Navigation Bar 44 pts
Nav Bar Image /Toolbar Image up to 20 x 20 pts (transparent PNG)
Tab Bar 49 pts
Tab Bar Icon up to 30 x 30 pts (transparent PNGs)
Text Field 31 pts
Height of a view inside a navigation bar 416 pts
Height of a view inside a tab bar 411 pts
Height of a view inside a navbar and a tab bar 367 pts
Portrait Keyboard height 216 pts
Landscape Keyboard height 140 pts

Points vs. Pixels

The iPhone 4 introduced a high resolution display with twice the pixels of previous iPhones. However you don't have to modify your  code to support high-res displays; the coordinate system goes by points rather than pixels, and the dimensions in points of the screen and all UI elements remain the same.
iOS 4 supports high resolution displays (like the iPhone 4 display) via the  scale property on UIScreen, UIView, UIImage, and CALayer classes. If the object is displaying high-res content, its scale property is set to 2.0. Otherwise it defaults to 1.0.
All you need to do to support high-res displays is to provide @2x versions of the images in your project. See the  checklist for updating to iOS4 or  Apple documentation for  Supporting High Resolution Screens for more info.

Adjusting Sizes

Click here to see how to adjust  View Frames and Bounds.

Additional References

  • Apple Documentation: Points vs. Pixels
  • Apple Documentation: UIBarButtonItem Class Reference says "Typically, the size of a toolbar and navigation bar image is 20 x 20 points."
  • Apple Documentation: UITabBarItem Class Reference says "The size of an tab bar image is typically 30 x 30 points."

7.iPhone之发送附件邮件代码

    MFMailComposeViewController *picker = [[MFMailComposeViewControlleralloc] init];
    picker.mailComposeDelegate = self;
    
    [pickersetSubject:@"I have a pencil for you"];
    NSString*databasePathFromApp = [[[NSBundle mainBundle] resourcePath]stringByAppendingPathComponent:@"10.pdf"];
    NSData*fileData = [NSDatadataWithContentsOfFile:databasePathFromApp];
    [pickeraddAttachmentData:fileData mimeType:@"application/pdf"fileName:@"15.pdf"];
    [pickeraddAttachmentData:fileData mimeType:@"application/pdf"fileName:@"16.pdf"];
    UIImage*roboPic = [UIImage imageNamed:@"1.png"];
    NSData*imageData = UIImageJPEGRepresentation(roboPic, 1);
    [pickeraddAttachmentData:imageData mimeType:@"image/jpg"fileName:@"1.png"];
    
    NSString*emailBody = @"This is a cool image of a robot Ifound.  Check it out!";
    [pickersetMessageBody:emailBody isHTML:YES];
    
    [selfpresentModalViewController:picker animated:YES];
    [pickerrelease];

8.iphone之自动休眠定时器

Phone OS试图省电的一个方法是使用自动休眠定时器。

如果在一定的时间内没有检测到触摸事件,系统最初会使屏幕变暗,并最终完全关闭屏幕。大多数开发者都应该让这个定时器打开,但是,游戏和不使用触摸输入的应用程序开发者可以禁用这个定时器,使屏幕在应用程序运行时不会变暗。

将共享的 UIApplication对象的 idleTimerDisabled属性设置为 YES,就可以禁用自动休眠定时器。

9.iphone之广告转向

NSString *iTunesLink =@"http://itunes.apple.com/us/app/id(产口id号)?mt=8";

[[UIApplication sharedApplication] openURL:[NSURLURLWithString:iTunesLink]];

10.iphone之navigationItem 添加标题视图的方法

但是如果题目太长,后半部分就变成省略号了,那要实现自定义字体。代码如下:
UILabel *titleText =[[UILabel alloc] initWithFrame: CGRectMake(0, 0,200, 20)];
titleText.backgroundColor =[UIColor clearColor];
[titleText setFont:[UIFontsystemFontOfSize:15.0]];
[titleText setText:@"设置navigationItem标题的字体大小"];
self.navigationItem.titleView=titleText;//titleView
[titleText release];

11.iphone之开源类库工具

几个常用的开源类库及下载地址:
1. jsonjson编码解码
2. GTMBase64base64编码解码
3. TouchXMLxml解析
4. SFHFKeychainUtils安全保存用户密码到keychain中
5. MBProgressHUD很棒的一个加载等待特效框架
6. ASIHTTPRequesthttp等相关协议封装
7. EGORefreshTableHeaderView下拉刷新代码
8. AsyncImageView异步加载图片并缓存代码

12.内存代码监测
//////////////////监测内存/////////////////////
- (double)availableMemory
{
    //防止私有被打回,先屏蔽
    vm_statistics_data_t vmStats;
    mach_msg_type_number_t infoCount = HOST_VM_INFO_COUNT;
    kern_return_t kernReturn = host_statistics(mach_host_self(), HOST_VM_INFO, (host_info_t)&vmStats, &infoCount);
    
    if(kernReturn != KERN_SUCCESS)
    {
        return NSNotFound;
    }
    return ((vm_page_size * vmStats.free_count) / 1024.0) / 1024.0;
}

-(void) outputmemory
{
    double power = [self availableMemory];
    printf("剩余内存 %ld M\n",(long) power);
}





欢迎喜欢交流和热心的iphone开发朋友加入qq群参与讨论:186739796,验证码:csdn。


 


你可能感兴趣的:(image,xcode,iPhone,documentation,XCode4,keyboard)