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);
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}};
UICommanddow* wd = [[UICommanddow alloc] initWithFrame:frame];
[wd setBackgroundcolor:[UIcolor clearcolor]];
[wd setCommanddowLevel:UICommanddowLevelStatusbar];
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] Commanddows];
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);
}
转自:http://blog.sina.com.cn/s/blog_6a2cbc930100m7eh.html
2、结合UIImageVIEw实现图片的移动和缩放
http://disanji.net/2011/02/19/iphone-uiimageview-move-and-zoom-images/
Easy life之二:容器
容器是让多数程序员又爱又恨的东西。在Cocoa中,容器是如此的简单易用以至于您一旦用过,就会对它们“爱不释手”。Cocoa中的容器类主要有这么几 个:Nsstring、NSArray、NSDictionary、NSSet和NSIndexSet等,它们都是Foundation Framework的一部分。
为什么人们会对Cocoa的容器“爱不释手”呢?
原因一:NSArray、NSDictionary、NSSet都不强制其内部元素类型的一致性。
举个简单的例子:
Nsstring *aString = [Nsstring stringWithString:@"This is a demo."];
NSNumber *aNumber = [NSNumber numberWithInteger:0];
NSArray *anArray = [[NSArray alloc] initWithObjects:aString,aNumber,nil];
在上述例子中,我们首先建立了一个Nsstring对象,然后又建立了一个NSNumber对象,最后我们将这两个Nsstring和NSNumber对象都“塞”到了一个NSArray对象中。
有够爽吧?连想都别想,什么东西都能往里面装(基本类型、结构体除外)!
原因二:容器类的“可修改”和“不可修改”
上面我们展示的Nsstring、NSArray、NSDictionary、NSSet以及NSIndexSet等,都是容器的“不可修改”的版本。所 谓的“不可修改”,指的是这个容器一旦被创建以后,我们就不可以通过代码修改它的集合。那如果我们需要修改(例如添加、删除、替换)这些容器的元素,该怎 么办呢?
Cocoa中几乎所有的容器类,都提供了另外一个“可修改”的版本。例如:继承自Nsstring的NSMutableString、继承自 NSArray的NSMutableArray、继承自NSDictionary的NSMutableDictioanry、继承自NSSet的 NSMutableSet以及继承自NSIndexSet的NSMutableIndexSet等。这些“可修改”的版本提供了简单直观的方法,用来修改 其内部的元素。例如:
Nsstring *aString = [Nsstring stringWithString:@"This is a demo."];
NSNumber *aNumber = [NSNumber numberWithInteger:0];
NSMutableArray *aMutableArray = [NSMutableArray array];
[aMutableArray addobject:aString];
[aMutableArray addobject:aNumber];
[aMutableArray removeObjectAtIndex:0];
[aMutableArray removeAllObjects];
在上述代码中,第一、二行建立了一个Nsstring对象和一个NSNumber对象。第三行建立了一个NSMutableArray对象(也就是一个 “可修改”的NSArray对象)。第四、五行通过-addobject:方法分别将第一、二行建立的Nsstring对象和NSNumber对象加入了 这个“可修改”的NSArray里。第六行则是根据我们给定的索引号0,删除了数组中的第一个元素。第七行的-removeAllObjects最后一口 气将数组中存在的所有元素统统删除(置空数组)。
是不是感觉一切都变的简单了许多呢?
那就赶快拿起Xcode,开始享受Cocoa编程所带来的快乐吧!
粉丝看Cocoa “Simple things simple,complex things possible.”(简单的事情更简单,复杂的事情也成为可能。) “C++ is to C as Lung Cancer is to Lang.”(C++之于C正如肺癌之于肺。) “We thought we would see how hard it would be to switch the code we had to use Cocoa Bindings. We rewrote everything in a day or two - I think we deleted over a thousand lines of code that just wasn’t needed any more.”(当初我们认为将原有的代码迁移到使用Cocoa Bindings的代码是一件非常艰苦的活。但事实上我们在仅仅一两天内就重写了所有的东西——我想我们删除了有超过一千行因为Cocoa Bindings而不再需要了的代码。) “Without Cocoa Bindings,it would have taken another four or five months,maybe more,to finish DelicIoUs library.”(要是没有Cocoa Bindings,完成DelicIoUs library将要再花上四、五个月的时间,或许更长。)
以上是内存溢出为你收集整理的iphone开发小技巧全部内容,希望文章能够帮你解决iphone开发小技巧所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)