2011年2月16日星期三

+ (UIImage *)imageNamed:(NSString *)name导致的内存问题

这种方法在application bundle的顶层文件夹寻找名字的图象 , 如果找到图片, 系统缓存图象。图片内容被加载到系统内存中,使用时直接引用到系统内存。
所以当图片比较大时,程序使用的内存会迅速上升导致内存警告并退出。
特别在使用Interface Builder建立界面时,如果直接拖动UIImageView 并设置image的图片名称。InterfaceBuilder 正是通过UIImage 类的imageName方法加载图片。图片被缓存,导致内存使用较大。且无法释放,即使release掉 UIImageView也无济于事。
所以推荐使用+ (UIImage *)imageWithContentsOfFile:(NSString *)path方法加载图片。

也可以重载 imageNamed方法。
@implementation UIImage(imageNamed_Hack)

+ (UIImage *)imageNamed:(NSString *)name {

    return [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@",
   [[NSBundle mainBundle] bundlePath], name ] ];

}

@end
Note: With this override you will not have any cache loading UIImages, if you need this,you will have to implement your own cache.
Tip: If your applications use much image processing, consider to use de PhotoshopFramework for iPhone. Check here: http://sourceforge.net/projects/photoshopframew/
Share:

2011年2月15日星期二

cocoa 下的正则表达式框架

RegexKit is an Objective-C framework for regular expressions:

  • Support for Mac OS X Cocoa and GNUstep. Mac OS X 10.4 or later required.

  • Mac OS X Universal Binary, including 64-bit support on Mac OS X 10.5.

  • No sub-classing required. Seamlessly adds regular expression support to all NSArray, NSData, NSDictionary, NSSet, and NSString Foundation objects with a rich set of Objective-C category additions.

  • Unicode enabled. Full Unicode support for NSString objects.

  • Extensive, high quality documentation.

  • Full source code with a BSD license.

  • Uses the BSD licensed PCRE Perl Compatible Regular Expressions library for the regular expression engine.


Tuned for high performance, including such features as:

  • Caches the compiled form of the regular expression for speed.

  • Multithreading safe, including multiple reader, single writer multithreaded access to the compiled regular expression cache.

  • Makes minimal use of heap storage (ie, malloc() and free()), instead allocating most temporary buffer needs dynamically from the stack.

  • Uses Core Foundation directly on Mac OS X for additional speed.


Includes support for Mac OS X 10.5 Leopard:

  • 64 bit support. Pre-built for ppc, ppc64, i386, and x86_64.

  • Garbage Collection enabled. Complete support for Leopards Garbage Collection feature.

  • Integrated Xcode 3.0 documentation. Get real time API information via the Research Assistant.

  • Collection of instruments for Instruments.app.

  • RegexKit specific DTrace probe points.


http://regexkit.sourceforge.net/

除了使用正则表达式匹配字符、数组以外,还可以使用NSPredicate做过滤查找。待续...
Share:

2011年2月11日星期五

iPhone中使用自定义字体

步骤:

1) 将字体文件添加到Resources目录.
2) 然后在你的工程的Info.plist文件中新建一行(Add Row),添加key为:UIAppFonts,类型为Array或Dictionary都行;在其下方添加新行   Value为XXX.ttf(你字体的名字)。

3) 添加的字体的文件名称是无关紧要的, 主要是要知道你添加的字体的fontname

如: msjh.ttf   (Window7中的微软正黑体)  , 加入UIAPPFonts
Family name: Microsoft JhengHei
Font name: MicrosoftJhengHeiRegular

则使用的话就需要使用 [UIFont fontWithName:@"Microsoft JhengHei" size:12];

获取程序内支持的字体列表的代码:

NSArray *familyNames = [[NSArray alloc] initWithArray:[UIFont familyNames]];
NSArray *fontNames;
NSInteger indFamily, indFont;
for (indFamily=0; indFamily<[familyNames count]; ++indFamily)
{
NSLog(@"Family name: %@", [familyNames objectAtIndex:indFamily]);
fontNames = [[NSArray alloc] initWithArray:
[UIFont fontNamesForFamilyName:
[familyNames objectAtIndex:indFamily]]];
for (indFont=0; indFont<[fontNames count]; ++indFont)
{
NSLog(@" Font name: %@", [fontNames objectAtIndex:indFont]);
}
[fontNames release];
}
[familyNames release];

Share: