在IOS 学习中经常会遇到一些小技巧,有时面试题中也会出现该技巧。比如我就遇到了输出代码行问题。
让UIImage有缩放功能
@interface UIImage (scale)
-(UIImage*)scaleToSize:(CGSize)size;
@end
#import "UIImage+Scale.h"
@implementation UIImage (scale)
-(UIImage*)scaleToSize:(CGSize)size
{
// 创建一个bitmap的context
// 并把它设置成为当前正在使用的context
UIGraphicsBeginImageContext(size);
// 绘制改变大小的图片
[self drawInRect:CGRectMake(0, 0, size.width, size.height)];
// 从当前context中创建一个改变大小后的图片
UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
// 使当前的context出堆栈
UIGraphicsEndImageContext();
// 返回新的改变大小后的图片
return scaledImage;
}
@end
截屏的方法:
UIGraphicsBeginImageContext(CGSizeMake(320, 300));
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);
控制log的输出
使用下面的代码, 可以定义一个DebugLog宏,在使用IPHONE模拟器时输出日志。日志的内容包含了时间、当前类、当前函数、当前行及其他参数信息。
我们可以修改TARGET_IPHONE_SIMULATOR替换为其他预定义,或者使用在项目中自定义的宏,以达到不同模式下控制日志的输出。
#if TARGET_IPHONE_SIMULATOR
#define DebugLog(log, ...) NSLog((@"%s [Line %d] " log), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);
#else
#define DebugLog(log, ...)
#endif
自定义宏的方法:打开项目属性, 找到Preprocessor Macros, 双击, 弹出窗口中点击+按钮即可定义.
xcode修改文件注释
修改公司名称:在Terminal中,输入
defaults write com.apple.Xcode PBXCustomTemplateMacroDefinitions '{"ORGANIZATIONNAME" = "company.com";}'
随机数的使用
#import <time.h>
#import <mach/mach_time.h>
srandom()的使用
srandom((unsigned)(mach_absolute_time() & 0xFFFFFFFF));
直接使用 random() 来调用随机数
在UIImageView 中旋转图像
float rotateAngle = M_PI;
CGAffineTransform transform =CGAffineTransformMakeRotation(rotateAngle);
imageView.transform = transform;
以上代码旋转imageView, 角度为rotateAngle, 方向可以自己测试哦!
在Quartz中如何设置旋转点
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg.png"]];
imageView.layer.anchorPoint = CGPointMake(0.5, 1.0);
这个是把旋转点设置为底部中间。记住是在QuartzCore.framework中才得到支持。
创建.plist文件并存储
NSString *errorDesc; //用来存放错误信息
NSMutableDictionary *rootObj = [NSMutableDictionary dictionaryWithCapacity:4]; //NSDictionary, NSData等文件可以直接转化为plist文件
NSDictionary *innerDict;
NSString *name;
Player *player;
NSInteger saveIndex;
for(int i = 0; i < [playerArray count]; i++) {
player = nil;
player = [playerArray objectAtIndex:i];
if(player == nil)
break;
name = player.playerName;// This "Player1" denotes the player name could also be the computer name
innerDict = [self getAllNodeInfoToDictionary:player];
[rootObj setObject:innerDict forKey:name]; // This "Player1" denotes the person who start this game
}
player = nil;
NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:(id)rootObj format:NSPropertyListXMLFormat_v1_0 errorDescription:&errorDesc];
红色部分可以忽略,只是给rootObj添加一点内容。这个plistData为创建好的plist文件,用其writeToFile方法就可以写成文件。下面是代码:
/*得到移动设备上的文件存放位置*/
NSString *documentsPath = [self getDocumentsDirectory];
NSString *savePath = [documentsPath stringByAppendingPathComponent:@"save.plist"];
/*存文件*/
if (plistData) {
[plistData writeToFile:savePath atomically:YES];
}
else {
NSLog(errorDesc);
[errorDesc release];
}
- (NSString *)getDocumentsDirectory {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
return [paths objectAtIndex:0];
}
读取plist文件并转化为NSDictionary
NSString *documentsPath = [self getDocumentsDirectory];
NSString *fullPath = [documentsPath stringByAppendingPathComponent:@"save.plist"];
NSMutableDictionary* plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:fullPath];
读取一般性文档文件
NSString *tmp;
NSArray *lines; /*将文件转化为一行一行的*/
lines = [[NSString stringWithContentsOfFile:@"testFileReadLines.txt"]
componentsSeparatedByString:@"\n"];
NSEnumerator *nse = [lines objectEnumerator];
// 读取<>里的内容
while(tmp = [nse nextObject]) {
NSString *stringBetweenBrackets = nil;
NSScanner *scanner = [NSScanner scannerWithString:tmp];
[scanner scanUpToString:@"<" intoString:nil];
[scanner scanString:@"<" intoString:nil];
[scanner scanUpToString:@">" intoString:&stringBetweenBrackets];
NSLog([stringBetweenBrackets description]);
}
对于读写文件,还有补充,暂时到此。随机数和文件读写在游戏开发中经常用到。所以把部分内容放在这,以便和大家分享,也当记录,便于查找。
隐藏NavigationBar
[self.navigationController setNavigationBarHidden:YES animated:YES];
在想隐藏的ViewController中使用就可以了。
navigationBar 自定义背景
@interface UINavigationBar (MyCustomNavBar)
@end
@implementation UINavigationBar (MyCustomNavBar)
- (void) drawRect:(CGRect)rect {
UIImage *barImage = [UIImage imageNamed:@"xxxxx.png"];
[barImage drawInRect:rect];
}
@end